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.早期的方式 在早期,组件要 ...
随机推荐
- MVC HTML页面使用
解决HTML <system.webServer> <validation validateIntegratedModeConfiguration="false" ...
- springboot 项目pom.xml文件基本配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- linux配置powerline(bash/vim)美化
安装powerline需要pip 链接:https://pan.baidu.com/s/1Jc59VD35PYic2fTK5v8h1w 密码:otfp pip curl https://bootstr ...
- 20155201 网络攻防技术 实验八 Web基础
20155201 网络攻防技术 实验八 Web基础 一.实践内容 Web前端HTML,能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. We ...
- bzoj 4008 亚瑟王 - 动态规划 - 概率与期望
Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 亮.众所周知,亚瑟王是一 ...
- 【python017--函数对象1】
一.函数 1.定义函数:def 函数名(): 2.调用函数:直接写函数的名称() >>> def MyFirstFunction(): print('this my firs ...
- ODAC(V9.5.15) 学习笔记(四)TOraQuery (1)
TOraQuery是ODAC中常用的一个组件,其继承关系如下: TDataSet ---TMemDataSet ---TCustomDADataSet ---TOraDataSet ---TCusto ...
- Bootstrap3基础 form-group 输入框之间出现间隔
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- Python3 tkinter基础 Button text,fg 按钮上显示的文字 文字的颜色
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- LOJ#2444. 「NOI2011」阿狸的打字机
题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有 \(28\) 个按键,分别印有 \(26\) 个小写英文字母和 B . P 两个字母. 经阿狸研究发现,这个打字机是 ...