SpringBoot中的@Import注解四種使用方式詳解
@Import注解四種使用方式
在介紹 @Import注解的使用之前,我們先看源碼:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import { /** * {@link Configuration @Configuration}, {@link ImportSelector}, * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import. */ Class<?>[] value(); }
從注釋來看,@Import注解只可以標(biāo)注在類上,可以結(jié)合 @Configuration注解、ImportSelector、ImportBeanDefinitionRegistrar一起使用,也可以導(dǎo)入普通的類。
因此,@Import的使用方式有4種:直接導(dǎo)入類,導(dǎo)入配置類來導(dǎo)入Bean,導(dǎo)入 ImportSelector 的實(shí)現(xiàn)類,導(dǎo)入 ImportBeanDefinitionRegister 的實(shí)現(xiàn)類。
需要注意的是:ImportSelector、ImportBeanDefinitionRegistrar 這兩個(gè)接口都必須依賴于 @Import 一起使用,而 @Import 可以單獨(dú)使用。
我們熟悉的 @EnableAsync 、@EnableCaching、@EnableScheduling 等都是借助 @Import 注解來實(shí)現(xiàn)的。
需要導(dǎo)入的類
public class Hello { private String msg; public void print() { if (msg != null) { System.out.println(msg); } else { System.out.println("hello word"); } } }
1、直接導(dǎo)入類
@Import(Hello.class) public class SpringTestApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); } }
項(xiàng)目中常用的方式如下:
@Import(Hello.class) @Configuration public class Config { }
2、導(dǎo)入配置類來導(dǎo)入Bean
@Configuration public class HelloConfiguration { @Bean public Hello createHello() { return new Hello(); } } @Import(HelloConfiguration.class) public class SpringTestApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); } }
這種方式使用較少,只要把配置類放到能被掃描到的包下,就可以去掉 @Import 導(dǎo)入。這種方式的使用場景是配置類無法被掃描到。
3、導(dǎo)入 ImportSelector 實(shí)現(xiàn)類
ImportSelector是一個(gè)接口,實(shí)現(xiàn)這個(gè)接口需要重寫selectImports方法。selectImports方法會(huì)返回一個(gè)String數(shù)組,它包含的元素是需要被導(dǎo)入到容器中的類的全限定名。
下面我們實(shí)現(xiàn) ImportSelector 接口并重寫 selectImports 方法,將Hello類的全限定名返回。
public class HelloImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { List list = new ArrayList<>(); list.add("a.b.domain.Hello"); return StringUtils.toStringArray(list); } } @Import(HelloImportSelector.class) public class SpringTestApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); } }
上面是導(dǎo)入指定的類,ImportSelector 還可以實(shí)現(xiàn)根據(jù)條件導(dǎo)入某個(gè)類,例如 @EnableAsync 可以根據(jù)屬性mode來選擇實(shí)現(xiàn)代理的方式。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AsyncConfigurationSelector.class) public @interface EnableAsync { Class<? extends Annotation> annotation() default Annotation.class; boolean proxyTargetClass() default false; AdviceMode mode() default AdviceMode.PROXY; int order() default Ordered.LOWEST_PRECEDENCE; } public class AsyncConfigurationSelector extends AdviceModeImportSelector { private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME = "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration"; @Override @Nullable public String[] selectImports(AdviceMode adviceMode) { switch (adviceMode) { case PROXY: return new String[] {ProxyAsyncConfiguration.class.getName()}; case ASPECTJ: return new String[] {ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME}; default: return null; } } } public abstract class AdviceModeImportSelector implements ImportSelector { ... }
像這種還不能決定注入哪個(gè)類,就可以實(shí)現(xiàn)此接口,根據(jù)條件或配置來注入合適的處理類。
4、導(dǎo)入 ImportBeanDefinitionRegistrar 實(shí)現(xiàn)類
當(dāng)類實(shí)現(xiàn)了 ImportBeanDefinitionRegistrar 接口,就可以拿到注冊(cè)器,可以向容器中注冊(cè)任何的Bean,并且可以對(duì)Bean添加屬性。
public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Hello.class) .addPropertyValue("msg", "test") .getBeanDefinition(); registry.registerBeanDefinition("hello", beanDefinition); } } @Import(HelloImportBeanDefinitionRegistrar.class) public class SpringTestApplication { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); } }
到此這篇關(guān)于SpringBoot中的@Import注解四種使用方式詳解的文章就介紹到這了,更多相關(guān)@Import注解四種使用方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis CURD及模糊查詢功能的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis CURD及模糊查詢功能的實(shí)現(xiàn),有查詢刪除,插入,更新功能,通過實(shí)例代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-06-06springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能
這篇文章主要介紹了springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能,需要的朋友可以參考下2017-12-12java webApp異步上傳圖片實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java webApp異步上傳圖片實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào)
這篇文章主要介紹了spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(控制臺(tái)版本)
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)控制臺(tái)版本的學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06