国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

@Import用來導(dǎo)入@Configuration注解的配置類、聲明@Bean注解的bean方法、導(dǎo)入ImportSelector的實(shí)現(xiàn)類或?qū)隝mportBeanDefinitionRegistrar的實(shí)現(xiàn)類。

@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();}

分析類注釋得出結(jié)論:

聲明一個bean 導(dǎo)入@Configuration注解的配置類 導(dǎo)入ImportSelector的實(shí)現(xiàn)類 導(dǎo)入ImportBeanDefinitionRegistrar的實(shí)現(xiàn)類

@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 {}

導(dǎo)入@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 {}

導(dǎo)入ImportSelector的實(shí)現(xiàn)類

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 {}

導(dǎo)入ImportBeanDefinitionRegistrar的實(shí)現(xiàn)類

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 {}

最后,我們來看下導(dǎo)入結(jié)果:

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('--------------------------------------------------------'); }}

打印結(jié)果如下:

--------------------------------------------------------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種導(dǎo)入方法被導(dǎo)入SpringIOC容器中。

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品欧洲一区二区三区 | 精品综合 | 欧美成 人h版在线观看 | 国产成人亚洲欧美三区综合 | 国产三级精品久久三级国专区 | 俄罗斯一级毛片免费播放 | 国产成a人片在线观看视频 国产成版人视频网站免费下 | 鲁丝片一区二区三区免费 | 97视频免费在线观看 | 热99re久久精品香蕉 | 国产精品久久久久三级 | 亚洲精品在线播放 | 欧美 亚洲 中文字幕 | 久草手机在线观看视频 | 台湾香港澳门三级在线 | 国产a级特黄的片子视频免费 | 玖玖玖视频在线观看视频6 玖玖影院在线观看 | 91久久精一区二区三区大全 | 国产精品久久久久久免费 | 精品亚洲成a人在线观看 | 亚洲精品国产第一区二区三区 | 成人二区| 女人国产香蕉久久精品 | 亚洲一级免费视频 | 久久久久久久岛国免费观看 | 欧美成人毛片一级在线 | 日本在线视频观看 | 久久欧美精品欧美久久欧美 | 韩国精品欧美一区二区三区 | 毛片在线视频在线播放 | 国产八区| 亚洲精品国产精品国自产 | 免费真实播放国产乱子伦 | 黄频漫画 | free性丰满白嫩白嫩的hd | 欧美一区综合 | 久久精品国产影库免费看 | 欧美操操操 | 亚洲成人国产精品 | 亚洲精品久久九九精品 | 久久精品国产精品亚洲 |