通俗的讲:该注解放在setter方法上,表示当前的setter修饰的属性必须在Spring.xml中进行装配,否则报错BeanInitializationException异常,所以这是个检查注解。
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;}}
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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="派大星"/><!-- 这里没有装配age属性,所以项目运行会报错 --></bean></beans>
报错:“ Property 'age' is required for bean 'student' ”
