過(guò)濾以排除...
下面的代碼定義了兩個(gè)Java bean。
package com.m.o2fo.common; public class JobTitle { @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
員工豆
package com.m.o2fo.common; public class Employee{ private String name; private String address; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Employee [name=" + name + ", address=" + address + ", age=" + age + "]"; } }
要啟用@Autowired,我們必須注冊(cè)“AutowiredAnnotationBeanPostProcessor"。我們可以通過(guò)兩種方式做到這一點(diǎn)。
這里是添加Spring上下文和< context:annotation-config />的代碼。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="myJobTitle" class="com.m.o2fo.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp" class="com.m.o2fo.common.Employee"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
以下代碼顯示如何包含“AutowiredAnnotationBeanPostProcessor"在bean配置文件中。
<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-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="myJobTitle" class="com.m.o2fo.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp" class="com.m.o2fo.common.Employee"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
這里是要運(yùn)行的代碼。
package com.m.o2fo.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); JobTitle cust = (JobTitle)context.getBean("myJobTitle"); System.out.println(cust); } }
以下代碼顯示如何通過(guò)@Autowired在setter方法中自動(dòng)布線bean
package com.m.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } @Autowired public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
以下代碼顯示了如何通過(guò)@Autowired在構(gòu)造函數(shù)上自動(dòng)布線bean。
package com.m.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired public JobTitle(Employee person) { this.person = person; } @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
以下代碼顯示了如何通過(guò)@Autowired在字段上自動(dòng)布線bean。
package com.m.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired private Employee person; private int type; private String action; }
所有上述三種方法都將自動(dòng)連接的Employee Java bean轉(zhuǎn)換為JobTitle的person屬性。
這里是運(yùn)行上面代碼的代碼。
package com.m.o2fo.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); JobTitle cust = (JobTitle)context.getBean("myJobTitle"); System.out.println(cust); } }
輸出:
默認(rèn)情況下,@Autowired注釋執(zhí)行依賴性檢查以確保自動(dòng)連接的豆存在。
如果Spring不能找到匹配的bean,它將拋出異常。要禁用@Autowired依賴性檢查,請(qǐng)將@Autowired的“required"屬性設(shè)置為false。
import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired(required=false) private Employee person; }
@Qualifier注釋允許我們選擇Java bean在字段上執(zhí)行自動(dòng)連接。
我們需要@Qualifier注釋?zhuān)?dāng)我們有兩個(gè)合格的bean來(lái)自動(dòng)導(dǎo)線。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="myJobTitle" class="com.m.o2fo.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp1" class="com.m.o2fo.common.Employee"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> <bean id="myEmp2" class="com.m.o2fo.common.Employee"> <property name="name" value="java2s2" /> <property name="address" value="address 2" /> <property name="age" value="28" /> </bean> </beans>
在上面的xml代碼配置文件中,我們有兩個(gè)com.m.o2fo.common.Employee實(shí)例。 如果我們不指定哪個(gè)自動(dòng)連接到JobTitle,他們都符合自動(dòng)連線操作。
要選擇要使用的Java bean,我們可以使用@Qualifier注釋。
package com.m.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class JobTitle { @Autowired @Qualifier("myEmp1") private Employee person; }
更多建議: