@Required注解适用于bean属性的setter方法,使用@Required的方法必须在xml中填充,负责报错

例如下面的例子中,student中的setAge和setName有@Required注解

如果xml中没有使用<property name="age" value="11">就会报错,

package com.crm;
import org.springframework.beans.factory.annotation.Required;
public class Student {
private Integer age;
private String name;
@Required
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
@Required
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package com.crm;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); System.out.println("Name : " + student.getName() );
System.out.println("Age : " + student.getAge() );
}
}
<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for student bean -->
<bean id="student" class="com.crm.Student">
<property name="name" value="Zara" /> <!-- try without passing age and check the result -->
<!-- property name="age" value="11"-->
</bean> </beans>

最新文章

  1. html-fieldset线中嵌套字符
  2. quad 和 plane 区别是什么?
  3. log_reuse_wait_desc为REPLICATION,日志暴大,无法收缩
  4. 开源分布式实时计算引擎 Iveely Computing 之 安装部署(2)
  5. 回调的代理(delegate)实现
  6. Linux some command(continue...)
  7. javascript 键盘事件总结
  8. sql server存储过程相关
  9. out ref区别
  10. 安装Ubuntu小计
  11. Cookie例子
  12. Remoting接口测试工具
  13. Web VLC 设置 tcp 协议播放
  14. HDU1039 Easier Done Than Said?
  15. Windows搭建golang开发平台
  16. vue table 固定首列和首行
  17. MySQL union all排序问题
  18. 解决Windows 系统下Chrome中有多个音频界面时 无法静音单个Tab界面的问题
  19. Django框架----路由系统、视图和模板(简单介绍)
  20. linux c server and client 简单的通信

热门文章

  1. Windows下oracle-win-64-11g安装步骤
  2. Linux中各种压缩文件
  3. centos 下安装pdo_pgsql 只需一个命令_______yum install php56w-pgsql
  4. Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)
  5. bd存储
  6. HDU - 5703 Desert 【找规律】
  7. 每天一个Linux命令(9)mv命令
  8. Java多线程系列 JUC线程池04 线程池原理解析(三)
  9. python作用域和js作用域的比较
  10. 使用vsftp搭建ftp服务