@Required注解
基于注解的配置
使用注解来配置依赖注入,而不用XML来描述一个bean连线。
如果在Spring使用注解,则需要这样配置:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><!-- bean definitions go here -->
</beans>
这样Spring就会自动连接值到属性,方法和构造函数。
@Required注解
@Required注解应用于bean属性的setter方法,它表明影响的bean属性在配置时必须放在XML配置文件中。
拿一个最简单的例子来说明:
main:
public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");Person person = (Person) context.getBean("person");person.setName("张三");person.setAge(11);System.out.println("Name : " + person.getName() );System.out.println("Age : " + person.getAge() );}
}
Person类:
public class Person {private Integer age;private String name;@Requiredpublic void setAge(Integer age) {this.age = age;}public Integer getAge() {return age;}@Requiredpublic void setName(String name) {this.name = name;}public String getName() {return name;}
}
Beans.xml:
<!-- Definition for student bean --><bean id="person" class="com.tutorialspoint.Person"><!-- <property name="name" value="张三" />--><!-- property name="age" value="11">--></bean>
这样运行会有怎样的结果?是不是觉得应该是
Name:张三
Age:11
实际上这会导致一个 BeanInitializationException
的异常,是因为我们在name和age设置了 @Required
注解,此时的 person
的属性就必须是在XML配置文件下,也就是上面xml中注释的部分。
我们将注释去掉,并且在Main类中将 person.setName("张三");person.setAge(11);
加上注释,这样就可以熟悉的结果了:
@Required注解相关推荐
- 使用@required注解完成依赖检查
使用@required注解完成依赖检查: 即在对象属性的set方法上添加 @required后,若 <bean>配置中不为该属性赋值,运行会报错. 第一步: package com.xuz ...
- Spring使用@Required注解依赖检查
Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种情况,你需要 @Required ...
- Spring 基于注解(annotation)的配置之@Required注解
从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...
- Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用...
Spring @Autowired,@Resource,@Required注解的用法和作用 Spring中 @Autowired标签与 @Resource标签 的区别 Spring注解@Compone ...
- Java注解研究之@Required
Created by Wang, Jerry on Jul 27, 2016 @Requiredpublic void setChineseAddressService(final AddressSe ...
- 关于Spring中的context:annotation-config/配置(开启注解)
转自:https://www.cnblogs.com/doudouxiaoye/p/5681518.html 当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些B ...
- spring学习12 -Spring 框架模块以及面试常见问题注解等
以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式-在AOP和remoting中被用的比较 ...
- spring @Required
spring的依赖检查功能: 在bean的xml配置文件中 : dependency-check=simple / Object / all ,检查的所有属性,非必init的属性也会被报错 @Requ ...
- spring3.0注解
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- Spring学习(8)--- @Autowired注解(一)
可以将@Autowired注解为"传统"的setter方法 package com.mypackage;import org.springframework.beans.facto ...
最新文章
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
- linux支持sshd救援模式,单用户、救援模式、克隆、两机互联
- 以操作系统的角度述说线程与进程
- 推荐10个很棒的 CSS3 开发工具
- Github的简单使用
- vue实现首屏加载等待动画 避免首次加载白屏尴尬
- 深夜福利, 小试linuxkit
- HDU-5935 Car 逆推 贪心 精度
- Leetcode--671. 合并二叉树
- 一篇博客读懂设计模式之---模板方法模式
- Linux E325: ATTENTION Found a swap file by the name “./.backu.sh.swp“
- 三菱PLC支持哪种OPC服务器,OPC-访问三菱Q系列PLC方法
- 激活函数详解/为什么要用激活函数
- docker安装sentinel的超简单步骤
- 安卓怎么打开html游戏,安卓手机如何打开.xapk文件
- 《FLEX工程师成长计划》--入门篇之一:关于此书
- 查看当前计算机CPU架构
- 量化交易1-backtrader介绍
- Java实现等额本息
- 工作一到五年的Java程序员该如何提升自己?