@required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 xml 配置文件中,否则容器就会抛出一个 beaninitializationexception 异常。下面显示的是一个使用 @required 注释的示例。
让我们使 eclipse ide 处于工作状态,请按照下列步骤创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为 springexample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。 |
2 | 使用 add external jars 选项添加所需的 spring 库文件,就如在 spring hello world example 章节中解释的那样。 |
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