Spring可以做依賴檢查以確保已設(shè)置或注入所需的屬性。
Spring可以做依賴檢查以確保已設(shè)置或注入所需的屬性。...
以下部分使用兩個(gè)Java bean如下所示以顯示如何使用依賴性檢查。
客戶類。
package com.www.o2fo.common; public class Customer { private Person person; private int type; private String action; 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; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } @Override public String toString() { return "Customer [person=" + person + ", type=" + type + ", action=" + action + "]"; } }
人員類
package com.www.o2fo.common; public class Person { 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 "Person [name=" + name + ", address=" + address + ", age=" + age + "]"; } }
下面的代碼顯示了如何使用spring 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 id="myCustomer" class="com.www.o2fo.common.Customer" > <property name="action" value="buy" /> </bean> <bean id="myPerson" class="com.www.o2fo.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
依賴關(guān)系檢查的默認(rèn)值為none。如果沒有明確定義依賴性檢查模式,它的默認(rèn)值為“none",因此不會執(zhí)行依賴性檢查。
依賴關(guān)系檢查的默認(rèn)值為none。如果沒有明確定義依賴性檢查模式,它的默認(rèn)值為“none",因此不會執(zhí)行依賴性檢查。...
<bean id="myCustomer" class="com.www.o2fo.common.Customer" dependency-check="simple">
完整配置文件。
<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 id="myCustomer" class="com.www.o2fo.common.Customer" dependency-check="simple"> <property name="person" ref="myPerson" /> <property name="action" value="buy" /> </bean> <bean id="myPerson" class="com.www.o2fo.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
完整配置文件。...
“type"屬性是類型int,它是一個(gè)基本類型尚未設(shè)置,一個(gè)UnsatisfiedDependencyException將拋出如下。
在下面我們將介紹另一種進(jìn)行依賴性檢查的方法。
我們可以使用@Required Annotation為Java bean添加依賴性檢查。
@Required注釋可應(yīng)用于特定屬性。
以下Customer對象在setPerson()方法中具有@Required以確保person屬性已設(shè)置。
package com.www.o2fo.common; import org.springframework.beans.factory.annotation.Required; public class Customer { private Person person; private int type; private String action; public Person getPerson() { return person; } @Required public void setPerson(Person 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; } }
在對方法應(yīng)用@Required注釋之后,我們還需要注冊一個(gè)RequiredAnnotationBeanPostProcessor以確認(rèn)bean配置文件中的@Required注釋。
有兩種方法可以啟用RequiredAnnotationBeanPostProcessor。
這里是上下文的語法:annotation-config。
<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="CustomerBean" class="com.www.o2fo.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.www.o2fo.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
以下xml代碼顯示如何包含“RequiredAnnotationBeanPostProcessor"在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.RequiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.www.o2fo.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.www.o2fo.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
如果我們運(yùn)行它,將拋出以下錯(cuò)誤消息,因?yàn)槲丛O(shè)置person屬性。
org.springframework.beans.factory.BeanInitializationException: Property "person" is required for bean "CustomerBean"
我們可以定義自定義注釋,通過使用Spring進(jìn)行依賴性檢查@必需樣式注釋。
在下面的示例中,我們將創(chuàng)建一個(gè)名為@Mandatory的自定義@必需類型的注釋,這相當(dāng)于@Required注釋。
在下面的示例中,我們將創(chuàng)建一個(gè)名為@Mandatory的自定義@必需類型的注釋,這相當(dāng)于@Required注釋。...
package com.www.o2fo.common; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Mandatory { }
然后,我們將新創(chuàng)建的注釋應(yīng)用于來自Java Bean的屬性。
package com.www.o2fo.common; public class Customer { private Person person; private int type; private String action; @Mandatory public void setPerson(Person person) { this.person = person; } }
最后,我們需要在xml配置文件中注冊它包括@Mandatory注釋在“RequiredAnnotationBeanPostProcessor"類中。
<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.RequiredAnnotationBeanPostProcessor"> <property name="requiredAnnotationType" value="com.www.o2fo.common.Mandatory"/> </bean> <bean id="CustomerBean" class="com.www.o2fo.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
更多建議: