Spring @Required注释
来源:互联网 发布:写安卓软件 编辑:程序博客网 时间:2024/06/03 21:30
@Required注解适用于bean属性的setter方法并且它指示,受影响的bean属性必须在配置时被填充在XML配置文件中,否则容器将抛出BeanInitializationException例外。下面是一个例子,说明使用@ Required注解。
例子:
我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
这里是Student.java文件的内容:
package com.yiibai;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 age; } @Required public void setName(String name) { this.name = name; } public String getName() { return name; }}
以下是MainApp.java文件的内容:
package com.yiibai;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); System.out.println("Name : " + student.getName() ); System.out.println("Age : " + student.getAge() ); }}
以下是配置文件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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for student bean --> <bean id="student" class="com.yiibai.Student"> <property name="name" value="Zara" /> <!-- try without passing age and check the result --> <!-- property name="age" value="11"--> </bean></beans>
一旦创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将抛出BeanInitializationException异常,并与其他日志信息打印以下错误:
Property 'age' is required for bean 'student'
接下来,可以从“age”属性中删除注释如下尝试后,上面的例子:
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for student bean --> <bean id="student" class="com.yiibai.Student"> <property name="name" value="Zara" /> <property name="age" value="11"/> </bean></beans>
现在,上面的例子会产生以下结果:
Name : ZaraAge : 11
标签:Spring @Required 注释 @Required
本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Spring @Required注释
本文地址:http://www.yiibai.com/spring/spring_required_annotation.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Spring @Required注释
本文地址:http://www.yiibai.com/spring/spring_required_annotation.html
0 0
- Spring @Required注释
- Spring Autowired(required = false) 说明与@Qualifier 注释说明
- Spring注释@Autowired @Resource @Qualifier @Required @Resource @Component,@Service,@Controller,@Reposit
- spring事务之REQUIRED
- Spring关于@required注解
- spring事务传播机制-REQUIRED嵌套REQUIRED
- Spring 注释
- spring 注释
- spring 注释
- spring 注释
- Spring @Required 注解检查属性
- Spring @Required 注解检查属性
- Spring 3.0 Required注解检查
- spring事务REQUIRED REQUIRES_NEW区别
- spring 中的@required的作用
- Spring传播机制-Propagation.REQUIRED
- spring中出现dataSource is required异常
- Spring Autowired(required = false) 说明
- Android Studio实战技巧
- 37. Element appendChild() 方法
- android 5.0新特性学习总结之下拉刷新(一)
- ASP.Net在web.config中设置上传文件的大小方法 (转)
- 诚信
- Spring @Required注释
- IOS TableView详解(二)
- 学PHP那点事?
- php中abstract和interface的区别
- C#中泛型的详细介绍
- 最大子段和部分问题(DP)
- Java常用集合元素使用小结
- eclipse 导入项目后中文乱码问题
- JS函数全集