以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-annotation.html

@Required注解适用于bean属性setter方法,并表示受影响的bean属性必须在XML配置文件在配置时进行填充。否则,容器会抛出一个BeanInitializationException异常。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testannotationrequired</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testannotationrequired</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

Student.java:

package com.jsoft.testspring.testannotationrequired;

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 this.age;
} public void setName(String name){
this.name = name;
} public String getName(){
return this.name;
}
}

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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"> <context:annotation-config/> <bean id="student" class="com.jsoft.testspring.testannotationrequired.Student">
<property name="name" value="Jim"/>
</bean> </beans>

App.java:

package com.jsoft.testspring.testannotationrequired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student)applicationContext.getBean("student");
System.out.println(student.getAge());
System.out.println(student.getName());
}
}

而此时的运行结果是出现了错误的,因为age的setter方法没有在bean中注入,而age的setter方法标记了@Required,也就是必须要输入,抛出的异常为:BeanInitializationException。

那么我们将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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="student" class="com.jsoft.testspring.testannotationrequired.Student">
<property name="name" value="Jim"/>
<property name="age" value="27"/>
</bean> </beans>

此时的运行结果一切正常:

spring @Required注解的更多相关文章

  1. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  2. 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier

    Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...

  3. Spring的@Required注解

    该@Required注解适用于bean属性setter方法,并表示受影响的bean属性必须在XML配置文件在配置时进行填充.否则,容器会抛出一个BeanInitializationException异 ...

  4. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  5. Spring使用@Required注解依赖检查

    Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种情况,你需要 @Required ...

  6. Spring学习(7)--- @Required注解

    @Required注解是用于bean属性的setter方法 这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在bean定义胡通过自动装配一个明确的属性值 package com.mypa ...

  7. spring IOC 注解@Required

    @Required注解适用于bean属性的setter方法,使用@Required的方法必须在xml中填充,负责报错 例如下面的例子中,student中的setAge和setName有@Require ...

  8. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  9. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

随机推荐

  1. 查看oracle/mysql数据库版本号

    1.1.       ORACLE 软件版本 使用oracle用户登录,输入echo "select * from v\$version;"|sqlplus -S / as sys ...

  2. 查看weblgic/Tuxedo/WebSpere(WAS)/Tomcat中间件版本

    1.中间件  1.1       Weblogic WebLogic的版本信息.JDK信息.参数配置均可通过控制台查看. 软件版本 [命令]more /weblogic/bea/logs/log.tx ...

  3. SQL Server 2012链接服务器

    第一步,在服务器对象,链接服务器那里新建链接服务器:输入要链接服务器的IP地址. 第二步,安全性输入连接数据的用户名和密码. 第三步,验证是不可以打开链接服务器库的表名,视图等数据. 第四步,如果要查 ...

  4. 第六章 Fisco Bcos 多服务器分布式部署

    想了解相关区块链开发,技术提问,请加QQ群:538327407 前提概要 前面几章,我们通过单机部署,在单台服务器上搭建四个节点,完成Fisco Bcos 底层搭建,并完成相关合约开发.sdk 开发. ...

  5. Asp.net HttpClient Proxy(Fiddler)

    <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault=& ...

  6. IIS下网站对options请求直接返回404

    什么是options请求 options请求为发送非简单跨域请求前的预检请求,若该请求未正常返回,浏览器会阻止后续的请求发送. 一般情况下,有三种方式会导致浏览器发起预检请求 1.请求的方法不是GET ...

  7. 记2017沈阳ICPC

    2017沈阳ICPC 10月20日 早上十点抵达沈阳,趁着老师还没到,跑去故宫游玩了一下,玩到一点多回到宾馆,顺便吃了群里大佬说很好吃的喜家德虾饺(真的好好吃),回到宾馆后身体有点不舒服了,头晕晕的, ...

  8. Dokcer基础使用总结(Dockerfile、Compose、Swarm)

    Dokcer基础 查看Linux版本 uname -r 查看Linux详尽信息 cat /etc/*elease CentOS Linux release (Core) NAME="Cent ...

  9. idea下maven命令打包不同配置

    1. 场景描述 不知道大家有没有遇到过?Idea集成的maven,在切换不同环境配置的时候,有时候反应很慢,还存在切换后打包还是原配置的情况. 2. 问题解决 通过在idea下执行maven命令进行切 ...

  10. Bzoj 1537: [POI2005]Aut- The Bus 题解 [由暴力到正解]

    1537: [POI2005]Aut- The Bus Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 264[Submit][S ...