Spring使用@Required注解依赖检查
@Required示例
package com.yiibai.common;import org.springframework.beans.factory.annotation.Required;public class Customer
{private Person person;private int type;private String action;public Person getPerson() {return person;}@Requiredpublic void setPerson(Person person) {this.person = person;}
}
1. 包函 <context:annotation-config />
<beans ...xmlns:context="http://www.springframework.org/schema/context"...http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">...<context:annotation-config />...
</beans>
完整的实例,
<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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans>
2. 包函 RequiredAnnotationBeanPostProcessor
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean>
</beans>
org.springframework.beans.factory.BeanInitializationException: Property 'person' is required for bean 'CustomerBean'
请阅读本文有关如何创建新的自定义@Required-style 注解。
Spring使用@Required注解依赖检查相关推荐
- Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- Spring的属性依赖检查
Spring支持4种依赖检查:默认的是none none – No dependency checking. simple – If any properties of primitive type ...
- 使用@required注解完成依赖检查
使用@required注解完成依赖检查: 即在对象属性的set方法上添加 @required后,若 <bean>配置中不为该属性赋值,运行会报错. 第一步: package com.xuz ...
- Spring 基于注解(annotation)的配置之@Required注解
从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...
- Spring注解依赖注入的三种方式的优缺点以及优先选择
当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 代码示例: Con ...
- 分享Spring中Bean的4种依赖检查模式
http://developer.51cto.com/art/201104/253227.htm 下面我们来看看Spring中的Bean的4中依赖检查模式:simple,object,all,none ...
- Spring5——(一)spingIOC(入门介绍,spring创建bean,依赖,注入,注解方式)
为什么要有框架? (1)对于web层来说,一个大型的程序往往需要编写大量的servlet,并且取值封装会非常繁琐. (2)对于dao层,要编写大量的sql语句,对于结果的解析也很麻烦,并且sql的复用 ...
- Spring 3.0 注解注入详解
Spring 3.0 注解注入详解 2011-04-15 09:44 17ZOUGUO ITEYE博客 我要评论(1) 字号:T | T AD: 一.各种注解方式 1.@Autowired注解(不推荐 ...
- spring dao层注解_Spring– DAO和服务层
spring dao层注解 欢迎来到Spring教程的第三部分. 在这一部分中,我们将继续编写Timesheet应用程序,这次我们将实现DAO层,业务服务并编写一些测试. 在上一部分中,我们定义了Ge ...
最新文章
- 十九、约束作用及常见约束
- ajax从mysql提取数据在html中_Python骚操作,提取pdf文件中的表格数据!
- 大规模数据处理开源软件
- 定积分计算器_使用科学计算器计算概率分布
- HttpWatch的Result中出现Aborted的原因分析[配图]
- elasticsearch7使用指导
- 15岁中国学生斩获苹果WWDC奖学金:写代码只用了2个周末
- Flex App的Size和Link报告
- 获取位置_原神蜥蜴尾巴怎么获得 蜥蜴尾巴获取位置分享
- ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
- SpringCloud第八章:Gateway新一代网关
- 逻辑回归分类——信用卡诈骗检测!这才是干货!
- WindowsServer修改用户密码
- 护眼灯买什么样的好?这几款2022年最佳的护眼灯值得一看!
- 人工智能专业计算机毕业设计选题推荐
- 【Python】EasyGUI实例——实现NJUCM绩点计算器程序
- 视频压缩编码和音频压缩编码基本原理
- 系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
- 全自动叠片式过滤器结构、原理说明
- 宽带隙器件增强电机控制设计