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 ...
随机推荐
- Win10 及 Google 浏览器显示界面异常
win10 和 google 界面显示异常 win10 个别 ui 组件花屏,google 界面直接黑屏 解决方式 更新集成显卡或者重装显卡驱动,最好使用 驱动人生 !!!
- Mac文件上传下载到服务器指定命令
下载文件夹 scp -r 远程登录服务器用户名@远程服务器ip地址:/下载文件夹的目录 『空格』 本地目录 下载文件 scp 远程登录服务器用户名@远程服务器ip地址:/下载文件的 ...
- Mybatis自动生成插件对数据库类型为text的处理
2019独角兽企业重金招聘Python工程师标准>>> 如果数据库中的字段为text或者blob这种大文本类型,在使用MybatisGenerator工具自动生成代码的时候会将其进行 ...
- 阿里云ECS安装JAVA+MYSQL+NGINX
2019独角兽企业重金招聘Python工程师标准>>> 1.准备工作 查看linux版本: linux版本为CentOS 7.4 查看系统信息: 系统为64位 确保服务器系统处于最新 ...
- js特效:鼠标滑过图片时切换为动图
效果展示 事前准备 一张普通的静态图+与其对应的gif图. 实现思路 获取图片的src,改变其后缀,使其变成与之对应的gif图片.(很简单有木有= =) 具体实现 编写html代码 <div c ...
- ACM算法--枚举方法(指数枚举,组合枚举)模板
// 递归实现指数型枚举 vector<int> chosen; void calc(int x) { if (x == n + 1) { for (int i = 0; i < c ...
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...
- 10 微信小程序路由跳转
一.四种跳转方式 API路由详解 除了tabBar这种底部跳转的方法,我们还有路由跳转,以下四种方式: 1. wx.switchTab() :跳转到 tabBar 页面,并关闭其他所有非 tabBar ...
- 分治思想--快速排序解决TopK问题
----前言 最近一直研究算法,上个星期刷leetcode遇到从两个数组中找TopK问题,因此写下此篇,在一个数组中如何利用快速排序解决TopK问题. 先理清一个逻辑解决TopK问题→快速排序→递 ...
- qt creator源码全方面分析(4-2)
目录 global头文件 global.h xx.h global头文件 插件的本质就是动态链接库,对于库,需要导出符号,供用户导入使用.在qt creator的源码中,存在固定的导入导出模式. gl ...