java中@Configuration使用場(chǎng)景
一、簡(jiǎn)單介紹
@Configuration注解可以標(biāo)注到類上,當(dāng)標(biāo)注到類上時(shí),啟動(dòng)Spring就會(huì)自動(dòng)掃描@Configuration注解標(biāo)注的類,將其注冊(cè)到IOC容器中,并被實(shí)例化成Bean對(duì)象。
如果被@Configuration注解標(biāo)注的類中存在使用@Bean注解標(biāo)注的創(chuàng)建某個(gè)類對(duì)象的方法,那么,Spring也會(huì)自動(dòng)執(zhí)行使用@Bean注解標(biāo)注的方法,將對(duì)應(yīng)的Bean定義信息注冊(cè)到IOC容器,并進(jìn)行實(shí)例化。
二、注解說(shuō)明
@Configuration注解是從Spring 3.0版本開始加入的一個(gè)使Spring能夠支持注解驅(qū)動(dòng)開發(fā)的標(biāo)注型注解,主要用于標(biāo)注在類上。當(dāng)某個(gè)類標(biāo)注了@Configuration注解時(shí),表示這個(gè)類是Spring的一個(gè)配置類。@Configuration注解能夠替代Spring的applicationContext.xml文件,并且被@Configuration注解標(biāo)注的類,能夠自動(dòng)注冊(cè)到IOC容器并進(jìn)行實(shí)例化。
1. @Configuration源碼
源碼詳見:org.springframework.context.annotation.Configuration。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(annotation = Component.class)
String value() default "";
// Since: 5.2
boolean proxyBeanMethods() default true;
// Since: 6.0
boolean enforceUniqueMethods() default true;
}
@Configuration注解中每個(gè)屬性的含義如下所示:
- value:存入到Spring IOC容器中的Bean的id。
- proxyBeanMethods:從Spring 5.2版本開始加入到@Configuration注解,表示被@Configuration注解標(biāo)注的配置類是否會(huì)被代理,并且在配置類中使用@Bean注解生成的Bean對(duì)象在IOC容器中是否是單例對(duì)象,取值為true或者false。當(dāng)取值為true時(shí),表示full(全局)模式,此模式下被@Configuration注解標(biāo)注的配置類會(huì)被代理,在配置類中使用@Bean注解注入到IOC容器中的Bean對(duì)象是單例模式,無(wú)論調(diào)用多少次被@Bean注解標(biāo)注的方法,返回的都是同一個(gè)Bean對(duì)象。當(dāng)取值為false時(shí),表示lite(輕量級(jí))模式,此模式下被@Configuration注解標(biāo)注的配置類不會(huì)被代理,在配置類中使用@Bean注解注入到IOC容器中的Bean對(duì)象不是單例模式,每次調(diào)用被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回一個(gè)新的Bean對(duì)象。默認(rèn)的取值為true。
- enforceUniqueMethods:從Spring 6.0開始加入到@Configuration注解,指定使用@Bean注解標(biāo)注的方法是否需要具有唯一的方法名稱,取值為true或者false。當(dāng)取值為true時(shí),表示使用@Bean注解標(biāo)注的方法具有唯一的方法名稱,并且這些方法名稱不會(huì)重疊。當(dāng)取值為false時(shí),表示使用@Bean注解標(biāo)注的方法名稱不唯一,存在被重疊的風(fēng)險(xiǎn)。默認(rèn)取值為true。
從@Configuration注解的源碼也可以看出,@Configuration注解本質(zhì)上是一個(gè)@Component注解,所以,被@Configuration注解標(biāo)注的配置類本身也會(huì)被注冊(cè)到IOC容器中。同時(shí),@Configuration注解也會(huì)被@ComponentScan注解掃描到。
2. @Configuration使用場(chǎng)景
基于Spring的注解開發(fā)應(yīng)用程序時(shí),可以將@Configuration注解標(biāo)注到某個(gè)類上。當(dāng)某個(gè)類被@Configuration注解標(biāo)注時(shí),說(shuō)明這個(gè)類是配置類,可以在這個(gè)類中使用@Bean注解向IOC容器中注入Bean對(duì)象,也可以使用@Autowired、@Inject和@Resource等注解來(lái)注入所需的Bean對(duì)象。
注意:基于Spring的注解模式開發(fā)應(yīng)用程序時(shí),在使用AnnotationConfigApplicationContext類創(chuàng)建IOC容器時(shí),需要注意如下事項(xiàng):
(1)如果調(diào)用的是AnnotationConfigApplicationContext類中傳入Class類型可變參數(shù)的構(gòu)造方法來(lái)創(chuàng)建IOC容器,表示傳入使用@Configuration注解標(biāo)注的配置類的Class對(duì)象來(lái)創(chuàng)建IOC容器,則標(biāo)注到配置類上的@Configuration注解可以省略。
如果傳入的配置類上省略了@Configuration注解,則每次調(diào)用配置類中被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回不同的Bean實(shí)例對(duì)象。
AnnotationConfigApplicationContext類中傳入Class類型可變參數(shù)的構(gòu)造方法源碼如下所示:
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
this();
register(componentClasses);
refresh();
}
(2)如果調(diào)用的是AnnotationConfigApplicationContext類中傳入String類型可變參數(shù)的構(gòu)造方法來(lái)創(chuàng)建IOC容器,表示傳入應(yīng)用程序的包名來(lái)創(chuàng)建IOC容器,則標(biāo)注到配置類上的@Configuration注解不能省略。
AnnotationConfigApplicationContext類中傳入String類型可變參數(shù)的構(gòu)造方法源碼如下所示:
public AnnotationConfigApplicationContext(String... basePackages) {
this();
scan(basePackages);
refresh();
}
三、 使用案例
1. 驗(yàn)證proxyBeanMethods屬性的作用
proxyBeanMethods屬性可取值為true或者false。取值為true時(shí),無(wú)論調(diào)用多少次在被@Configuration注解標(biāo)注的類中被@Bean注解標(biāo)注的方法,返回的都是同一個(gè)Bean對(duì)象。取值為false時(shí),每次調(diào)用在被@Configuration注解標(biāo)注的類中被@Bean注解標(biāo)注的方法,都回返回不同的Bean對(duì)象。
1.1 驗(yàn)證proxyBeanMethods取值為true的情況
(1)創(chuàng)建Person類
Person類主要是用來(lái)注冊(cè)到IOC容器中,并實(shí)例化對(duì)象。
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)創(chuàng)建ConfigurationAnnotationConfig類
ConfigurationAnnotationConfig類的作用就是充當(dāng)程序啟動(dòng)的配置類,會(huì)在ConfigurationAnnotationConfig類上標(biāo)注@Configuration注解,說(shuō)明ConfigurationAnnotationConfig類是Spring啟動(dòng)時(shí)的配置類。
@Configuration
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
可以看到,在ConfigurationAnnotationConfig類上標(biāo)注了@Configuration注解,由于@Configuration注解中的proxyBeanMethods屬性默認(rèn)為true,所以在ConfigurationAnnotationConfig類上的@Configuration注解省略了proxyBeanMethods屬性。
(3)創(chuàng)建ConfigurationAnnotationTest類
ConfigurationAnnotationTest類的作用就是整個(gè)案例程序的啟動(dòng)類,對(duì)整個(gè)案例程序進(jìn)行測(cè)試。
public class ConfigurationAnnotationTest {
? ? private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationAnnotationTest.class);
? ? public static void main(String[] args) {
? ? ? ? AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigurationAnnotationConfig.class);
? ? ? ? ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
? ? ? ? Person person1 = config.person();
? ? ? ? Person person2 = config.person();
? ? ? ? LOGGER.info("person1 == person2 ===>> {}", (person1 == person2));
? ? }
}可以看到,在ConfigurationAnnotationTest類的main()方法中,首先基于AnnotationConfigApplicationContext常見了IOC容器context,從context中獲取了ConfigurationAnnotationConfig類的Bean實(shí)例對(duì)象config,接下來(lái),調(diào)用兩次config的person()方法分別賦值給Person類型的局部變量person1和person2,最后打印person1是否等于person2的日志。
(4)測(cè)試案例
運(yùn)行ConfigurationAnnotationTest類的main()方法,輸出的結(jié)果信息如下所示。
person1 是否等于 person2 ===>> true
通過輸出的結(jié)果信息可以看出,person1是否等于person2輸出的結(jié)果為true。說(shuō)明當(dāng)@Configuration注解中的proxyBeanMethods屬性為true時(shí),每次調(diào)用使用@Configuration注解標(biāo)注的類中被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回同一個(gè)Bean實(shí)例對(duì)象。
1.2 驗(yàn)證proxyBeanMethods取值為false的情況
驗(yàn)證@Configuration注解中的proxyBeanMethods屬性為false的情況,與驗(yàn)證proxyBeanMethods屬性為true的情況的案例程序基本一致,只是將ConfigurationAnnotationConfig類上標(biāo)注的@Configuration注解的proxyBeanMethods屬性設(shè)置為false,案例實(shí)現(xiàn)的具體步驟如下所示。
(1)修改proxyBeanMethods屬性的值
@Configuration(proxyBeanMethods = false)
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
(2)測(cè)試案例
運(yùn)行ConfigurationAnnotationTest類的main()方法,輸出的結(jié)果信息如下所示。
person1 是否等于 person2 ===>> false
從輸出的結(jié)果信息可以看出,person1是否等于person2輸出的結(jié)果為false。說(shuō)明當(dāng)@Configuration注解中的proxyBeanMethods屬性為false時(shí),每次調(diào)用使用@Configuration注解標(biāo)注的類中被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回不同的Bean實(shí)例對(duì)象。
2. 傳入配置類創(chuàng)建IOC容器
調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入配置類的Class對(duì)象創(chuàng)建IOC容器時(shí),可以省略配置類上的@Configuration注解,案例的具體實(shí)現(xiàn)步驟如下所示。
(1)刪除@Configuration注解
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
(2)測(cè)試案例
運(yùn)行ConfigurationAnnotationTest類的main()方法,輸出的結(jié)果信息如下所示。
person1 是否等于 person2 ===>> false
從輸出的結(jié)果信息可以看到,輸出了person1是否等于person2的結(jié)果為false。說(shuō)明調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入配置類的Class對(duì)象創(chuàng)建IOC容器時(shí),可以省略配置類上的@Configuration注解,此時(shí)每次調(diào)用配置類中被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回不同的Bean實(shí)例對(duì)象。
3. 傳入包名創(chuàng)建IOC容器
調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入包名創(chuàng)建IOC容器時(shí),不能省略配置類上的@Configuration注解,案例的具體實(shí)現(xiàn)步驟如下所示。
(1)修改測(cè)試類
修改ConfigurationAnnotationTest類的main()方法中,創(chuàng)建AnnotationConfigApplicationContext對(duì)象的代碼,將調(diào)用傳入Class對(duì)象的構(gòu)造方法修改為調(diào)用傳入String對(duì)象的方法,修改后的代碼如下所示。
public class ConfigurationAnnotationTest {
? ? private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationAnnotationTest.class);
? ? public static void main(String[] args) {
? ? ? ? AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.lwk.demo.spring.annocation.configuration.config");
? ? ? ? ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
? ? ? ? Person person1 = config.person();
? ? ? ? Person person2 = config.person();
? ? ? ? LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
? ? }
}(2)刪除@Configuration注解
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
(3)測(cè)試案例
運(yùn)行ConfigurationAnnotationTest類的main()方法,可以看到程序拋出了異常信息,如下所示。
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.binghe.spring.annotation.chapter01.configuration.config.ConfigurationAnnotationConfig' available
從輸出的結(jié)果信息可以看出,調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入包名創(chuàng)建IOC容器時(shí),不能省略配置類上的@Configuration注解,否則會(huì)拋出NoSuchBeanDefinitionException。
(4)添加@Configuration注解
@Configuration
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
(5)再次測(cè)試案例
再次運(yùn)行ConfigurationAnnotationTest類的main()方法,輸出的結(jié)果信息如下所示。
person1 是否等于 person2 ===>> true
從輸出的結(jié)果信息可以看到,輸出了person1是否等于person2的結(jié)果為true,再次說(shuō)明調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入包名創(chuàng)建IOC容器時(shí),不能省略配置類上的@Configuration注解。
到此這篇關(guān)于java中@Configuration使用場(chǎng)景的文章就介紹到這了,更多相關(guān)java @Configuration內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 通過聚合查詢實(shí)現(xiàn)elasticsearch的group by后的數(shù)量
這篇文章主要介紹了java 通過聚合查詢實(shí)現(xiàn)elasticsearch的group by后的數(shù)量,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
springboot如何接收get和post請(qǐng)求參數(shù)
這篇文章主要介紹了springboot如何接收get和post請(qǐng)求參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Java 注冊(cè)時(shí)發(fā)送激活郵件和激活的實(shí)現(xiàn)示例
這篇文章主要介紹了Java 注冊(cè)時(shí)發(fā)送激活郵件和激活的實(shí)現(xiàn)示例的相關(guān)資料,需要的朋友可以參考下2017-07-07
Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)
循環(huán)隊(duì)列 (Circular Queue) 是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問題。本文將帶大家詳細(xì)了解循環(huán)隊(duì)列如何實(shí)現(xiàn),需要的朋友可以參考一下2021-12-12
Java使用Queryable-pageable實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家介紹了Java如何使用Queryable-pageable從而實(shí)現(xiàn)分頁(yè)效果,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06

