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
廣告