通俗的讲:该注解放在setter方法上,表示当前的setter修饰的属性必须在Spring.xml中进行装配,否则报错BeanInitializationException异常,所以这是个检查注解。

    1. public class Student {
    2. private Integer age;
    3. private String name;
    4. public Integer getAge() {
    5. return age;
    6. }
    7. @Required //该注释放在的是set方法中,如果没有在xml配置文件中配置相关的属性,就会报错
    8. public void setAge(Integer age) {
    9. this.age = age;
    10. }
    11. }
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xmlns:tx="http://www.springframework.org/schema/tx"
    6. xmlns:context="http://www.springframework.org/schema/context"
    7. xsi:schemaLocation="
    8. http://www.springframework.org/schema/beans
    9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    10. http://www.springframework.org/schema/aop
    11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    12. http://www.springframework.org/schema/tx
    13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    14. http://www.springframework.org/schema/context
    15. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    16. <context:annotation-config/>
    17. <!-- student bean的定义 -->
    18. <bean id = "student" class="com.how2java.w3cschool.required.Student">
    19. <property name = "name" value="派大星"/>
    20. <!-- 这里没有装配age属性,所以项目运行会报错 -->
    21. </bean>
    22. </beans>
    1. 报错:“ Property 'age' is required for bean 'student'