本例子源于: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>

最后的结果输出为:

最新文章

  1. C#:结构
  2. C 和指针 学习随便
  3. susy 学习之进阶
  4. JVM的相关知识整理和学习--(转载)
  5. busying
  6. VirtualBox虚拟机剪贴板共享
  7. Java GC 专家系列3:GC调优实践
  8. [LeetCode OJ] Best Time to Buy and Sell Stock I
  9. git 在linux中的使用
  10. 2014阿里实习生面试题——mysql如何实现的索引
  11. 动态修改ViewPagerIndicator CustomTabPageIndicator Tab标签文字颜色
  12. DirectFB学习笔记一
  13. 不同WINDOWS平台下磁盘逻辑扇区的直接读写
  14. C# WMI 远程PC(开机、关机、重启)
  15. 【BZOJ】1969: [Ahoi2005]LANE 航线规划
  16. [原创]K8uac bypassUAC(Win7/Wi8/Win10) 过46款杀软影响所有Windows版本
  17. windows 安装tensorflow
  18. JAVA-JSP内置对象之out对象
  19. C# 最全的系统帮助类
  20. 剑指offer三十一之连数中1出现的次数(从1到n整数中1出现的次数

热门文章

  1. php开启xdebug扩展
  2. Golang实现二分查找法
  3. css列表list、表格table
  4. SQL语句的优化方法
  5. Spring Boot 2 (四):使用 Docker 部署 Spring Boot
  6. Kali系列之aircrack-ng wifi密码穷举
  7. android uboot中的mmc命令
  8. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
  9. bzoj 2527 Meteors - 整体二分 - 树状数组
  10. VC++ 使用CreateProcess创建新进程