Spring @Required 注解



@Required 注解应用于 Bean 属性的 setter 方法,它指示必须在配置时在 XML 配置文件中填充受影响的 Bean 属性。否则,容器将抛出 BeanInitializationException 异常。以下是一个示例,展示了 @Required 注解的使用。

示例

让我们准备一个可用的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序:

步骤 描述
1 创建一个名为 SpringExample 的项目,并在创建的项目的 src 文件夹下创建一个 com.tutorialspoint 包。
2 如“Spring Hello World 示例”章节中所述,使用 添加外部 JAR 选项添加所需的 Spring 库。
3 com.tutorialspoint 包下创建 Java 类 StudentMainApp
4 src 文件夹下创建 Bean 配置文件 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
spring_annotation_based_configuration.htm
广告