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;
}
@Required
public 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/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://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/beans
http://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使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring的@Required注解
该@Required注解适用于bean属性setter方法,并表示受影响的bean属性必须在XML配置文件在配置时进行填充.否则,容器会抛出一个BeanInitializationException异 ...
- Spring学习(十)-----Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- Spring的属性依赖检查
spring支持4种依赖检查:默认的是none none – No dependency checking. simple – If any properties of primitive type ...
- Spring注解依赖注入的三种方式的优缺点以及优先选择
当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...
- [Spring]@Autowired,@Required,@Qualifier注解
@Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...
- Spring学习笔记之依赖的注解(2)
Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...
- spring @Required注解
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-ann ...
随机推荐
- 清理oracle的用户中的日志垃圾以及修改sys用户的密码
清理oracle的用户中的日志垃圾1.进入:/opt/oracle/product/11g/network/admin目录2.注释掉listener.ora文件中的TRACE_LEVEL_LISTEN ...
- php 全文搜索解决方法
全套解决方案 xunsearch 一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-dev ...
- 【IT公司笔试面试】75道逻辑推理题及答案
[1]假设有一个池塘,里面有无穷多的水.现有2个空水壶,容积分别为5升和6升.问题是如何只用这2个水壶从池塘里取得3升的水. 由满6向空5倒,剩1升,把这1升倒5里,然后6剩满,倒5里面,由于5里面有 ...
- [ python ] 匿名函数和高阶函数
匿名函数 描述: 关键字 lambda 定义的函数 语法: 函数名 = lambda 参数:返回值 返回值: 函数返回结果值 实例: 一个参数的匿名函数: func = lambda ...
- Python+Selenium 自动化实现实例-实现文件下载
#coding=utf-8 from selenium import webdriver #实例化一个火狐配置文件 fp = webdriver.FirefoxProfile() #设置各项参数,参数 ...
- bootstrap使用前注意点和盒子模型
bootstrap注意事项: https://getbootstrap.com/docs/4.0/getting-started/introduction/#quick-start 盒子模型: htt ...
- js 正则验证多个邮箱,用;隔开的那种
var r = /^((([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6}\;))*(([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\. ...
- python元类:type和metaclass
python元类:type和metaclass python中一切皆对象,所以类本身也是对象.类有创建对象的能力,那谁来创建类的呢?答案是type. 1.用tpye函数创建一个类 class A(ob ...
- 移动App 的 ios11 和 iPhoneX 适配
原文链接:https://blog.csdn.net/lovelyelfpop/article/details/79460700 WKWebView 调用拨打电话功能 1.HTML 要标记添加标记电话 ...
- 检测浏览器对HTML5新input类型的支持
HTML5新增加了很多input元素类型,比如color,date,datetime,datetime-local,email,month,number,range,search,tel,time,u ...