在上一篇sPRing的五種自動裝配方式 教程中,我們了解到spring利用xml方式進行操作的五種自動裝配方式,而在這篇文章中我們將學習利用@Autowired注解的方式進行自動裝配
@Autowired注解自動裝配的三種方式
setter方式構造函數方式通過字段自動裝配方式第一步:創建bean
Customer.java
package com.main.autowrite.autowired.annotation;public class Customer { //即將自動裝配的屬性 private Person person; private int type; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; }}Person.java
package com.main.autowrite.autowired.annotation;public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}第二步:配置beans配置文件
<?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="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>第三步:注冊AutowiredAnnotationBeanPostProcessor
主要有以下兩種方式進行注冊
在beans配置文件中添加spring上下文和“context:annotation-config”標簽包含 AutowiredAnnotationBeanPostProcessorbean
第一種:添加 Spring 上下文和context:annotation-config在beans配置文件中。 更改你的beans配置文件如下
<?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"> <context:annotation-config /> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>第二種:包含AutowiredAnnotationBeanPostProcessor 更改你的beans配置文件如下
<?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 class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>第四步:使用@Autowired注解進行自動裝配
當你已經完成上述步驟后,此時你就可以使用@Autowired注解進行自動裝配了。 主要有以下三種方式:
通過setter方法通過constructor構造方法直接在字段屬性聲明時使用@Autowired注解進行自動裝配
第一種:通過setter方法 修改你的Customer類如下:
package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 private Person person; private int type; public Person getPerson() { return person; } @Autowired public void setPerson(Person person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; }}第二種:通過constructor構造方法 修改你的Customer類如下
package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 private Person person; private String type; @Autowired public Customer(Person person) { this.person = person; } //getter and setter methods //toString methods}第三種: 修改你的Customer類如下
package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 @Autowired private Person person; private String type; //getter and setter methods //toString methods}第五步:測試(上述第四步的三種裝配方式均可使用以下方式進行測試)
@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml"); Customer customer = (Customer)context.getBean("customer"); System.out.print(customer.toString()); }
結論:使用@Autowired注解實現自動裝配非常靈活,而且功能強大。 在這里可以思考一下,如果存在多個person bean,那么customer將會自動裝配哪一個??可以參考下一篇關于@Qualifier注解的使用
新聞熱點
疑難解答