Spring @Required 注解
@Required 注解适用于 bean 属性设置器方法,它指示受影响的 bean 属性必须在配置时填充到 XML 配置文件中。 否则,容器会抛出 BeanInitializationException 异常。 下面是一个例子来展示@Required 注解的使用。
示例
让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 在 com.tutorialspoint 包下创建 Java 类 Student 和 MainApp。 |
| 4 | 在 src 文件夹下创建 Beans 配置文件 Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
这是 Student.java 文件的内容 −
package com.tutorialspoint;
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.tutorialspoint;
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.tutorialspoint.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.tutorialspoint.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>
</beans>
上面的示例将产生以下结果 −
Name : Zara Age : 11

