本例子源於:W3Cschool,在此做一個記錄
@Required注釋為為了保證所對應的屬性必須被設置,@Required 注釋應用於 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時必須放在 XML 配置文件中,否則容器就會拋出一個 BeanInitializationException 異常。下面顯示的是一個使用 @Required 注釋的示例。直接的理解就是,如果你在某個java類的某個set方法上使用了該注釋,那么該set方法對應的屬性在xml配置文件中必須被設置,否則就會報錯!!!
例子如下:
Studnent.java
package com.how2java.w3cschool.required; import org.springframework.beans.factory.annotation.Required; public class Student { private Integer age; private String name; public Integer getAge() { return age; } @Required //該注釋放在的是set方法中,如果沒有在xml配置文件中配置相關的屬性,就會報錯
public void setAge(Integer age) { this.age = age; } public String getName() { return name; } @Required public void setName(String name) { this.name = name; } }
MainApp.java
package com.how2java.w3cschool.required; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beanrequired.xml"); Student student = (Student) context.getBean("student"); System.out.println("Name:" + student.getName()); System.out.println("age:" + student.getAge()); } }
第一次的xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- student bean的定義 -->
<bean id = "student" class="com.how2java.w3cschool.required.Student">
<property name = "name" value="派大星"/>
</bean>
</beans>
應該和注意到Student.java中的@Required注釋應用在了兩個set方法上,但是此時在xml的bean中只設置了一個屬性,因此從報錯,“ Property 'age' is required for bean 'student' ”
將對應的屬性設置加上后的xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- student bean的定義 -->
<bean id = "student" class="com.how2java.w3cschool.required.Student">
<property name = "name" value="派大星"/>
<property name = "age" value="5"/>
</bean>
</beans>
最后的結果輸出為: