Spring 的@Required注释
本例子源于: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>
最后的结果输出为:
Spring 的@Required注释的更多相关文章
- Spring @Required 注释
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个BeanInitializationExc ...
- 【转载-好文】使用 Spring 2.5 注释驱动的 IoC 功能
在 IBM Bluemix 云平台上开发并部署您的下一个应用. 开始您的试用 原文链接:https://www.ibm.com/developerworks/cn/java/j-lo-spring25 ...
- 使用 Spring 2.5 注释驱动的 IoC 功能
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- [Spring]@Autowired,@Required,@Qualifier注解
@Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...
- 使用 Spring 2.5 注释驱动的 IoC 功能(转)
基于注释(Annotation)的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,提供了完全基于注释配置 Bean.装配 Bean 的功能,您可以使用基于注释的 Spring IoC 替换 ...
- 15个Spring的核心注释示例
众所周知,Spring DI和Spring IOC是Spring Framework的核心概念.让我们从org.springframework.beans.factory.annotation和org ...
- 【Java】Spring之基于注释的容器配置(四)
注释是否比配置Spring的XML更好? 基于注释的配置的引入引发了这种方法是否比XML“更好”的问题.答案是每种方法都有其优点和缺点,通常,由开发人员决定哪种策略更适合他们.由于它们的定义方式,注释 ...
- @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationEx ...
- Spring 框架中注释驱动的事件监听器详解
事件交互已经成为很多应用程序不可或缺的一部分,Spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要 ...
随机推荐
- asyncio 学习
来自:https://www.syncd.cn/article/asyncio_article_02 一.asyncio之—-入门初探 通过上一篇关于asyncio的整体介绍,看过之后基本对async ...
- ELK学习笔记之F5利用ELK进行应用数据挖掘系列(1)-HTTP
0x00 概述 F5 BIGIP从应用角度位于网络结构的关键咽喉位置,可获取所有应用的流量,针对流量执行L7层处理,即便是TLS加密的流量也可以通过F5进行SSL offload.通过F5可以统一获取 ...
- Python3 格式化字符串
Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...
- 创建docker镜像的私有仓库
CentOS Linux release 7.2.1511 Docker version 17.03.1-ce 安装registry镜像 同时安装一个比较小的镜像alpine待会作测试用: # doc ...
- 【题解】Luogu CF86D Powerful array
原题传送门 裸的莫队啊,我博客里有对莫队较详细的介绍 这道题很简单,可以说是裸的模板 但是如何在已有的值上进行操作? 小学生应该都知道 那么转移就超级简单了qaq inline void add(re ...
- maven的使用记录
maven的使用记录 使用的版本为3.6.0. maven配置部署项目 在cmd命令行中切换到Maven项目的根目录,比如:D:/xxxwork/java/maven-test,然后执行命令:$ mv ...
- 主动攻击:利用ms08_067_netapi进行攻击
利用ms09_053_wins进行攻击 ms08_067漏洞 如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允许远程执行代码. 在 Microsoft Windows 2000.Win ...
- libcurl 设置代理,通过Fiddler可以进行抓包
转载:https://blog.csdn.net/jaryguo/article/details/53021923 转载:https://www.cnblogs.com/miantest/p/7289 ...
- 如果此表在它的 ChildRelation 集合中不是父表,则不能将关系添加到该集合中。
今天遇到这个问题头都大了,百度上也没找到解决方案,就自己在哪里沉思................ 终于皇天不负有心人,被我解决了! 这是调用ChildRelations.Add(“名字”,“父级”, ...
- img的基线对齐问题
http://blog.csdn.net/u011997156/article/details/44806523