Spring的@Required注解
2024-10-20 03:59:26
该@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>
此时的运行结果一切正常:
最新文章
- crontab用法与实例
- Gamma Gamma~!!!
- 【poj1009】 Edge Detection
- 【JavaScript】关于prototype
- URAL 1073 Square Country(DP)
- SQL 的一个技巧
- linux reboot命令
- 根据SVN的MESSAGE进行多版本输出,反向排序,真是曲折~~~啊
- Windows下连接php5.3+sql server2008
- hdu5115 Dire Wolf【区间dp】
- POJ 3321 Apple Tree DFS序+fenwick
- Perl资料
- CUDA编程模型之内存管理
- Python在Windows上安装配置测试
- TCP编程实践小结1
- BellmanFord 最短路
- EndNote基础教程
- Git简单操作命令
- (转)设置VMWARE通过桥接方式使用主机无线网卡上网
- angular5中使用echart的方法
热门文章
- 【UI设计】扁平化设计之流行色值
- mac苹果ping不通网络
- Thrift 文件的格式及可用的数据类型
- Exception in thread ";main"; java.lang.ClassCastException: $Proxy13
- CAE医疗综合视听中心管理系统
- Hadoop内部的限流机制
- Spark1.0.0 生态圈一览
- JBoss AS 7之基本配置和部署(The Return Of The King)
- iOS 推断设备为iPhone还是iPad
- windows命令行设置IP与DNS