Spring @Required 注释
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个BeanInitializationException 异常。
下面显示的是一个使用 @Required 注释的示例。
新建一个Spring项目
创建 Java 类 Student 和 MainApp
下面是 Student.java 文件的内容:
package hello;
import org.springframework.beans.factory.annotation.Required;
public class Student {
private int age;
String name;
@Required
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
@Required
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
下面是 MainApp.java 文件的内容:
package hello;
//import org.springframework.context.support.AbstractApplicationContext;
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());
}
}
下面是配置文件 Beans.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: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="hello.Student" >
<property name="name" value="fanqie"/>
<property name="age" value="20"/>
</bean>
</beans>
运行结果如下:
name: fanqie
age: 20
总结:
1、要在使用@Required 注释的java文件中引入:
import org.springframework.beans.factory.annotation.Required;
2、配置文件Beans.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: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/>
<!-- bean definitions go here -->
</beans>
3、 bean 属性的 setter 方法在配置时必须放在 XML 配置文件中,否则会出错
@Required
public void setAge(Integer age) {
this.age = age;
}
@Required
public void setName(String name) {
this.name = name;
}
如上面有两个setter方法,则age和nama属性都必须放在XML配置文件中。
每天学习一点点,每天进步一点点。
Spring @Required 注释的更多相关文章
- Spring 的@Required注释
本例子源于:W3Cschool,在此做一个记录 @Required注释为为了保证所对应的属性必须被设置,@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean ...
- Spring @Autowired 注释
@Autowired 注释可以在 setter 方法中被用于自动连接 bean. 你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素. 当 Spring遇到一 ...
- @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationEx ...
- Spring通过注释配置Bean2 关联关系
接着我们讲讲关联关系的配置,我们耳熟能详的MVC结构,Controller关联着Service,Service关联着UserRepository,接着上一节的代码,完成上诉功能 在Main方法里,我们 ...
- Spring错误——Spring xml注释——org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 10; cvc-complex-type.2.3: 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。
背景:配置spring xml,注释xml中文件元素 错误: Caused by: org.xml.sax.SAXParseException; lineNumber: 24; columnNumbe ...
- spring @Required注解
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-ann ...
- Spring JSR-250 注释
Spring还使用基于 JSR-250 注释,它包括 @PostConstruct 注释 @PreDestroy 注释 @Resource 注释 @PostConstruct 和 @PreDestro ...
- Spring @Qualifier 注释
可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配. 在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指 ...
- [Spring] Annotation注释
自动扫描: 在<beans>标签内, <context:annotation-config />允许使用注解 <context:component-scan base-p ...
随机推荐
- (转)ATOM介绍和使用
一,Atom介绍 Atom 是 Github 开源的文本编辑器,这个编辑器完全是使用Web技术构建的(基于Node-Webkit).启动速度快,提供很多常用功能的插件和主题,可以说Atom已经足以胜任 ...
- Redis介绍及字符串操作
一.前言 不同程序之间实现通信的方法? A.通过不同程序之间建立socket,实现通信.实际应用适用于使用broker,如RabbitMQ,ZeroMQ. B.通过Json,Pickle,利用文件的写 ...
- BigDecimal 01 - 在JAVA中怎么比较Double类型数据的大小?
2019独角兽企业重金招聘Python工程师标准>>> 非整型数,运算由于精度问题,可能会有误差,建议使用BigDecimal类型! double a = 0.001; doub ...
- CCF NOI1039 2的n次方
问题链接:CCF NOI1039 2的n次方. 时间限制: 1000 ms 空间限制: 262144 KB 题目描述 对于任意给定的n,计算2的n次方. 输入 输入整数n. 输出 输出2的n次方的值 ...
- Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good
跨字段实体搜索 » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 --返回所有的相关文档:精确率 --不返回无关文档.目的是在结果的 ...
- 打造livecd的注意事项
一:在CentOS.ks的定制脚本中,删除syslinux组件:出错提示: /usr/lib/python2.6/site-packages/imgcreate/errors.py:45: Depre ...
- 图论--2-SAT--Tarjan连通分量+拓扑排序O(N+M)模板
#include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...
- HDU 1233 最小生成树模板题,练练模板
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- python(configparser 模块)
1.下载安装 configparser 第三方模块 pip install configparser 2.读取配置文件 #配置文件内容如下 """ "D:/co ...
- 使用Python+TensorFlow2构建基于卷积神经网络(CNN)的ECG心电信号识别分类(二)
心律失常数据库 目前,国际上公认的标准数据库包含四个,分别为美国麻省理工学院提供的MIT-BIH(Massachusetts Institute of Technology-Beth Israel H ...