spring IOC 注解@Required
2024-08-27 10:11:03
@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>
最新文章
- html-fieldset线中嵌套字符
- quad 和 plane 区别是什么?
- log_reuse_wait_desc为REPLICATION,日志暴大,无法收缩
- 开源分布式实时计算引擎 Iveely Computing 之 安装部署(2)
- 回调的代理(delegate)实现
- Linux some command(continue...)
- javascript 键盘事件总结
- sql server存储过程相关
- out ref区别
- 安装Ubuntu小计
- Cookie例子
- Remoting接口测试工具
- Web VLC 设置 tcp 协议播放
- HDU1039 Easier Done Than Said?
- Windows搭建golang开发平台
- vue table 固定首列和首行
- MySQL union all排序问题
- 解决Windows 系统下Chrome中有多个音频界面时 无法静音单个Tab界面的问题
- Django框架----路由系统、视图和模板(简单介绍)
- linux c server and client 简单的通信
热门文章
- Windows下oracle-win-64-11g安装步骤
- Linux中各种压缩文件
- centos 下安装pdo_pgsql 只需一个命令_______yum install php56w-pgsql
- Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)
- bd存储
- HDU - 5703 Desert 【找规律】
- 每天一个Linux命令(9)mv命令
- Java多线程系列 JUC线程池04 线程池原理解析(三)
- python作用域和js作用域的比较
- 使用vsftp搭建ftp服务