色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

淺談Spring中@Import注解的作用和使用

瀏覽:31日期:2023-09-04 11:54:55

@Import用來導入@Configuration注解的配置類、聲明@Bean注解的bean方法、導入ImportSelector的實現類或導入ImportBeanDefinitionRegistrar的實現類。

@Import注解的作用

查看Import注解源碼

/** * Indicates one or more {@link Configuration @Configuration} classes to import. * * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML. * Only supported for classes annotated with {@code @Configuration} or declaring at least * one {@link Bean @Bean} method, as well as {@link ImportSelector} and * {@link ImportBeanDefinitionRegistrar} implementations. * * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes * should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired} * injection. Either the bean itself can be autowired, or the configuration class instance * declaring the bean can be autowired. The latter approach allows for explicit, * IDE-friendly navigation between {@code @Configuration} class methods. * * <p>May be declared at the class level or as a meta-annotation. * * <p>If XML or other non-{@code @Configuration} bean definition resources need to be * imported, use {@link ImportResource @ImportResource} * * @author Chris Beams * @since 3.0 * @see Configuration * @see ImportSelector * @see ImportResource */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Import { /** * The @{@link Configuration}, {@link ImportSelector} and/or * {@link ImportBeanDefinitionRegistrar} classes to import. */ Class<?>[] value();}

分析類注釋得出結論:

聲明一個bean 導入@Configuration注解的配置類 導入ImportSelector的實現類 導入ImportBeanDefinitionRegistrar的實現類

@Import注解的使用

聲明一個bean

package com.example.demo.bean;public class TestBean1 {}package com.example.demo;import com.example.demo.bean.TestBean1;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Import({TestBean1.class})@Configurationpublic class AppConfig {}

導入@Configuration注解的配置類

package com.example.demo.bean;public class TestBean2 {}package com.example.demo.bean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class TestConfig { @Bean public TestBean2 getTestBean2(){ return new TestBean2(); }}package com.example.demo;import com.example.demo.bean.TestBean1;import com.example.demo.bean.TestConfig;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Import({TestBean1.class,TestConfig.class})@Configurationpublic class AppConfig {}

導入ImportSelector的實現類

package com.example.demo.bean;public class TestBean3 {}package com.example.demo.bean;import org.springframework.context.annotation.ImportSelector;import org.springframework.core.type.AnnotationMetadata;public class TestImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{'com.example.demo.bean.TestBean3'}; }}package com.example.demo;import com.example.demo.bean.TestBean1;import com.example.demo.bean.TestConfig;import com.example.demo.bean.TestImportSelector;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Import({TestBean1.class,TestConfig.class,TestImportSelector.class})@Configurationpublic class AppConfig {}

導入ImportBeanDefinitionRegistrar的實現類

package com.example.demo.bean;public class TestBean4 {}package com.example.demo.bean;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.RootBeanDefinition;import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;import org.springframework.core.type.AnnotationMetadata;public class TestImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(TestBean4.class); registry.registerBeanDefinition('TestBean4', rootBeanDefinition); }}package com.example.demo;import com.example.demo.bean.TestBean1;import com.example.demo.bean.TestConfig;import com.example.demo.bean.TestImportBeanDefinitionRegistrar;import com.example.demo.bean.TestImportSelector;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Import({TestBean1.class,TestConfig.class,TestImportSelector.class,TestImportBeanDefinitionRegistrar.class})@Configurationpublic class AppConfig {}

最后,我們來看下導入結果:

package com.example.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.test.context.junit4.SpringRunner;import java.util.Arrays;@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests { @Test public void test() { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class); String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames(); System.out.println('--------------------------------------------------------'); for (String beanDefinitionName: beanDefinitionNames) { System.out.println(beanDefinitionName); } System.out.println('--------------------------------------------------------'); }}

打印結果如下:

--------------------------------------------------------org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalRequiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactoryappConfigcom.example.demo.bean.TestBean1com.example.demo.bean.TestConfiggetTestBean2com.example.demo.bean.TestBean3TestBean4--------------------------------------------------------

可以看出TestBean1,TestBean2,TestBean3,TestBean4通過不同的4種導入方法被導入SpringIOC容器中。

到此這篇關于淺談Spring中@Import注解的作用和使用的文章就介紹到這了,更多相關Spring @Import注解內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美精品v日韩精品v国产精品 | 亚洲欧美日韩在线一区 | 国内精品伊人久久久影视 | 免费观看欧美一区二区三区 | 国产成人黄色 | 亚洲国产精品日韩在线 | 孕妇交| 久久精品国产这里是免费 | 香蕉国产人午夜视频在线 | 久久五月女厕所一区二区 | 国产精品成人在线播放 | 日韩a毛片免费全部播放完整 | 国产精品福利社 | 国产成人精品无缓存在线播放 | 日韩免费高清一级毛片 | 飘花国产午夜精品不卡 | 成人亚洲在线观看 | 免费一级毛片在线播放视频 | 欧美一级大片在线观看 | 国产三级麻豆 | 人成18亚洲资源在线 | 国产成人福利美女观看视频 | 大片毛片女女女女女女女 | 国产三级做爰在线观看 | 精品国产免费观看久久久 | www.亚洲视频 | 呦女亚洲一区精品 | 成人福利在线 | 亚洲天堂男人的天堂 | 成人欧美一区在线视频在线观看 | 丝袜美腿在线不卡视频播放 | 国产精品欧美一区二区三区不卡 | 成人禁在线观看网站 | 毛片中文字幕 | 青久草视频 | 夜精品a一区二区三区 | 一本色综合 | 特级a毛片| 看成年女人免费午夜视频 | 欧美日韩精品国产一区在线 | 久久高清免费 |