SpringBoot @Import與@Conditional注解使用詳解
說(shuō)明:基于atguigu學(xué)習(xí)筆記。
在了解spring boot自動(dòng)配置原理前,再來(lái)了解下兩個(gè)注解@Import注解和@Conditional注解。
@Import
@Import注解主要用于導(dǎo)入某些特殊的Bean,這些特殊的Bean和Bean Definitaion 有關(guān)。
主要用于導(dǎo)入@Configuration 類(lèi),ImportSelector和ImportBeanDefinitionRegistrar接口的實(shí)現(xiàn)類(lèi)。
@Import可以添加在@SpringBootApplication(啟動(dòng)類(lèi))、@Configuration(配置類(lèi))、@Component(組件類(lèi))對(duì)應(yīng)的類(lèi)上。
一般在Spring boot 中配置都一般都是自動(dòng)導(dǎo)入的,所以我們不需要使用@Import,但是如果你你想導(dǎo)入的配置類(lèi)不在自動(dòng)掃包路徑,那么該配置類(lèi)就需要使用@Import導(dǎo)入,尤其是第三方j(luò)ar包的配置類(lèi)都需要借助@Import來(lái)導(dǎo)入。
這里我們首先熟悉基本語(yǔ)法,具體的用法后面再學(xué)。
示例:
還使用上一章里項(xiàng)目示例:@Configuration注解和@Bean注解
使用 @Import導(dǎo)入我們需要的類(lèi),這里導(dǎo)入我們自己創(chuàng)建的類(lèi)和第三方logback jar包里的類(lèi),如下:
config類(lèi):
package com.example.ethan.config;
import ch.qos.logback.core.db.DBHelper;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods=true)
public class ConfigDemo {
@Bean
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user組件依賴(lài)了Dept組件
zhangsan.setDept(rd());
return zhangsan;
}
@Bean("my rd")
public Dept rd(){
return new Dept("研發(fā)部");
}
}主程序
package com.example.ethan;
import ch.qos.logback.core.db.DBHelper;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import com.example.ethan.config.ConfigDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class EthanApplication {
public static void main(String[] args) {
// 返回ioc容器
ConfigurableApplicationContext run = SpringApplication.run(EthanApplication.class, args);
// 只查看User類(lèi)型的Bean
String[] beanDefinitionNames = run.getBeanNamesForType(User.class);
System.out.println("========================");
for (String name : beanDefinitionNames) {
System.out.println(name);
}
DBHelper bean = run.getBean(DBHelper.class);
System.out.println(bean);
// com.example.ethan.bean.User
//user01
//ch.qos.logback.core.db.DBHelper@67207d8a
}
}可以看到,使用 @Import注解,會(huì)給容器中自動(dòng)創(chuàng)建出這兩個(gè)類(lèi)型的Bean,且Bean的名字是全類(lèi)名。
@Conditional
滿(mǎn)足Conditional指定的條件,則進(jìn)行注入指定Bean。
@Conditional是一個(gè)根注解,下面還派生了許多其他注解

@Conditional注解可以加在方法上也可以加在類(lèi)上。當(dāng)加在類(lèi)上,當(dāng)條件不成立,類(lèi)中所有代碼都不執(zhí)行,如果加在方法上,則只這個(gè)方法不執(zhí)行。
下面以@ConditionalOnBean(條件是判斷是否含有某個(gè)Bean)為例,演示代碼如下:
首先放在方法上:
package com.example.ethan.config;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods=true)
public class ConfigDemo {
@Bean("my rd")
public Dept rd(){
return new Dept("研發(fā)部");
}
@Bean
@ConditionalOnBean(name = "my rd")
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user組件依賴(lài)了Dept組件
zhangsan.setDept(rd());
return zhangsan;
}
}主程序:
package com.example.ethan;
import ch.qos.logback.core.db.DBHelper;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import com.example.ethan.config.ConfigDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class EthanApplication {
public static void main(String[] args) {
// 返回ioc容器
ConfigurableApplicationContext run = SpringApplication.run(EthanApplication.class, args);
boolean my_rd = run.containsBean("my rd");
System.out.println("容器中含有my rd這個(gè)Bean:" + my_rd);
boolean user01 = run.containsBean("user01");
System.out.println("容器中含有user01這個(gè)Bean:" + user01);
}
}這時(shí)候兩個(gè)好結(jié)果都是true,因?yàn)榘凑枕樞蜃⑷隑ean時(shí),先注入了"my rd"這個(gè)Bean到容器,然后在注入user01時(shí),檢查容器中含有my rd"這個(gè)Bean,就把user01也注入了。
如果把條件里name改為不存在的如"my rd02",那么入user01就不會(huì)注入容器。
下面把注解放到類(lèi)上:
package com.example.ethan.config;
import com.example.ethan.bean.Dept;
import com.example.ethan.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods=true)
@ConditionalOnBean(name = "my rd")
public class ConfigDemo {
@Bean("my rd")
public Dept rd(){
return new Dept("研發(fā)部");
}
@Bean
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user組件依賴(lài)了Dept組件
zhangsan.setDept(rd());
return zhangsan;
}
}這時(shí)候兩個(gè)Bean都不會(huì)注入容器,因?yàn)樵趫?zhí)行這個(gè)類(lèi)時(shí)檢查條件不成立。
這里對(duì)@ConditionalOnBean有個(gè)初步了解,具體何種用法類(lèi)似,可舉一反三。
參考:
到此這篇關(guān)于SpringBoot @Import與@Conditional注解使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot @Import與@Conditional內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Gateway網(wǎng)關(guān)自定義攔截器的不可重復(fù)讀取數(shù)據(jù)問(wèn)題
這篇文章主要介紹了Gateway網(wǎng)關(guān)自定義攔截器的不可重復(fù)讀取數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Spring中過(guò)濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系解析
在我們?nèi)粘5拈_(kāi)發(fā)中,我們經(jīng)常會(huì)用到Filter和Interceptor,這篇文章主要介紹了Spring中過(guò)濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系?,需要的朋友可以參考下2022-10-10
Spring中@Configuration注解和@Component注解的區(qū)別詳解
這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過(guò)如下一個(gè)案例,在不分析源碼的情況下,小伙伴們先來(lái)直觀(guān)感受一下這兩個(gè)之間的區(qū)別,需要的朋友可以參考下2023-09-09
Java多線(xiàn)程案例實(shí)戰(zhàn)之定時(shí)器的實(shí)現(xiàn)
在Java中可以使用多線(xiàn)程和定時(shí)器來(lái)實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線(xiàn)程案例之定時(shí)器實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Java實(shí)現(xiàn)布隆過(guò)濾器的示例詳解
布隆過(guò)濾器(Bloom?Filter)是1970年由布隆提出來(lái)的,實(shí)際上是由一個(gè)很長(zhǎng)的二進(jìn)制數(shù)組+一系列hash算法映射函數(shù),用于判斷一個(gè)元素是否存在于集合中。本文主要介紹了Java實(shí)現(xiàn)布隆過(guò)濾器的示例代碼,希望對(duì)大家有所幫助2023-03-03
SpringBoot使用JdbcTemplate訪(fǎng)問(wèn)操作數(shù)據(jù)庫(kù)基本用法
這篇文章主要介紹了SpringBoot使用JdbcTemplate訪(fǎng)問(wèn)操作數(shù)據(jù)庫(kù)基本用法,Spring對(duì)數(shù)據(jù)庫(kù)的操作在jdbc上s面做了深層次的封裝,使用spring的注入功能,可以把DataSource注冊(cè)到JdbcTemplate之中。下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-02-02

