在以前的Spring版本中,咱們只能經過寫XML配置文件來定義咱們的Bean,XML配置不只繁瑣,並且很容易出錯,稍有不慎就會致使編寫的應用程序各類報錯,排查半天,發現是XML文件配置不對!另外,每一個項目編寫大量的XML文件來配置Spring,也大大增長了項目維護的複雜度,每每不少個項目的Spring XML文件的配置大部分是相同的,只有不多量的配置不一樣,這也形成了配置文件上的冗餘。java
項目工程源碼已經提交到GitHub:https://github.com/sunshinelyz/spring-annotationgit
在Spring容器的底層,最重要的功能就是IOC和DI,也就是控制反轉和依賴注入。github
IOC:控制反轉,將類的對象的建立交給Spring類管理建立。
DI:依賴注入,將類裏面的屬性在建立類的過程當中給屬性賦值。
DI和IOC的關係:DI不能單獨存在,DI須要在IOC的基礎上來完成。spring
在Spring內部,全部的組件都會放到IOC容器中,組件之間的關係經過IOC容器來自動裝配,也就是咱們所說的依賴注入。接下來,咱們就使用註解的方式來完成容器組件的註冊、管理及依賴、注入等功能。數組
在介紹使用註解完成容器組件的註冊、管理及依賴、注入等功能以前,咱們先來看看使用XML文件是如何注入Bean的。bash
首先,咱們在工程的io.mykit.spring.bean包下建立Person類,做爲測試的JavaBean,代碼以下所示。微信
package io.mykit.spring.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.io.Serializable; /** * @author binghe * @version 1.0.0 * @description 測試實體類 */ @Data @ToString @NoArgsConstructor @AllArgsConstructor public class Person implements Serializable { private static final long serialVersionUID = 7387479910468805194L; private String name; private Integer age; }
接下來,咱們在工程的resources目錄下建立Spring的配置文件beans.xml,經過beans.xml文件將Person類注入到Spring的IOC容器中,配置以下所示。學習
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "person" class="io.mykit.spring.bean.Person"> <property name="name" value="binghe"></property> <property name="age" value="18"></property> </bean> </beans>
到此,咱們使用XML方式注入JavaBean就配置完成了。接下來,咱們建立一個SpringBeanTest類來進行測試,這裏,我使用的是Junit進行測試,測試方法以下所示。測試
@Test public void testXmlConfig(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); }
運行testXmlConfig()方法,輸出的結果信息以下。code
Person(name=binghe, age=18)
從輸出結果中,咱們能夠看出,Person類經過beans.xml文件的配置,已經注入到Spring的IOC容器中了。
經過XML文件,咱們能夠將JavaBean注入到Spring的IOC容器中。那使用註解又該如何實現呢?別急,其實使用註解比使用XML文件要簡單的多,咱們在項目的io.mykit.spring.plugins.register.config包下建立PersonConfig類,並在PersonConfig類上添加@Configuration註解來標註PersonConfig類是一個Spring的配置類,經過@Bean註解將Person類注入到Spring的IOC容器中。
package io.mykit.spring.plugins.register.config; import io.mykit.spring.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author binghe * @version 1.0.0 * @description 以註解的形式來配置Person */ @Configuration public class PersonConfig { @Bean public Person person(){ return new Person("binghe001", 18); } }
沒錯,經過PersonConfig類咱們就可以將Person類注入到Spring的IOC容器中,是否是很Nice!!主要咱們在類上加上@Configuration註解,並在方法上加上@Bean註解,就可以將方法中建立的JavaBean注入到Spring的IOC容器中。
接下來,咱們在SpringBeanTest類中建立一個testAnnotationConfig()方法來測試經過註解注入的Person類,以下所示。
@Test public void testAnnotationConfig(){ ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class); Person person = context.getBean(Person.class); System.out.println(person); }
運行testAnnotationConfig()方法,輸出的結果信息以下所示。
Person(name=binghe001, age=18)
能夠看出,經過註解將Person類注入到了Spring的IOC容器中。
到這裏,咱們已經明確,經過XML文件和註解兩種方式均可以將JavaBean注入到Spring的IOC容器中。那麼,使用註解將JavaBean注入到IOC容器中時,使用的bean的名稱是什麼呢? 咱們能夠在testAnnotationConfig()方法中添加以下代碼來獲取Person類型下的註解名稱。
//按照類型找到對應的bean名稱數組 String[] names = context.getBeanNamesForType(Person.class); Arrays.stream(names).forEach(System.out::println);
完整的testAnnotationConfig()方法的代碼以下所示。
@Test public void testAnnotationConfig(){ ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig.class); Person person = context.getBean(Person.class); System.out.println(person); //按照類型找到對應的bean名稱數組 String[] names = context.getBeanNamesForType(Person.class); Arrays.stream(names).forEach(System.out::println); }
運行testAnnotationConfig()方法輸出的結果信息以下所示。
Person(name=binghe001, age=18) person
那這裏的person是啥?咱們修改下PersonConfig類中的person()方法,將person()方法修改爲person01()方法,以下所示。
package io.mykit.spring.plugins.register.config; import io.mykit.spring.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author binghe * @version 1.0.0 * @description 以註解的形式來配置Person */ @Configuration public class PersonConfig { @Bean public Person person01(){ return new Person("binghe001", 18); } }
此時,咱們再次運行testAnnotationConfig()方法,輸出的結果信息以下所示。
Person(name=binghe001, age=18) person01
看到這裏,你們應該有種豁然開朗的感受了,沒錯!!使用註解注入Javabean時,bean在IOC中的名稱就是使用@Bean註解標註的方法名稱。咱們可不能夠爲bean單獨指定名稱呢?那必須能夠啊!只要在@Bean註解中明確指定名稱就能夠了。好比下面的PersonConfig類的代碼,咱們將person01()方法上的@Bean註解修改爲@Bean("person")註解,以下所示。
package io.mykit.spring.plugins.register.config; import io.mykit.spring.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author binghe * @version 1.0.0 * @description 以註解的形式來配置Person */ @Configuration public class PersonConfig { @Bean("person") public Person person01(){ return new Person("binghe001", 18); } }
此時,咱們再次運行testAnnotationConfig()方法,輸出的結果信息以下所示。
Person(name=binghe001, age=18) person
能夠看到,此時,輸出的JavaBean的名稱爲person。
結論:咱們在使用註解方式向Spring的IOC容器中注入JavaBean時,若是沒有在@Bean註解中明確指定bean的名稱,就使用當前方法的名稱來做爲bean的名稱;若是在@Bean註解中明確指定了bean的名稱,則使用@Bean註解中指定的名稱來做爲bean的名稱。
好了,我們今天就聊到這兒吧!別忘了給個在看和轉發,讓更多的人看到,一塊兒學習一塊兒進步!!
項目工程源碼已經提交到GitHub:https://github.com/sunshinelyz/spring-annotation
若是以爲文章對你有點幫助,請微信搜索並關注「 冰河技術 」微信公衆號,跟冰河學習Spring註解驅動開發。公衆號回覆「spring註解」關鍵字,領取Spring註解驅動開發核心知識圖,讓Spring註解驅動開發再也不迷茫。