Spring @Required 注解

  • @Required注解

    @Required注解适用于bean属性setter方法,它指示在配置时必须在XML配置文件中填充受影响的bean属性。 否则,容器将引发BeanInitializationException异常。 以下是显示@Required注解的用法的示例。
  • 实例

    假设我们拥有一个运行良好的Eclipse IDE,并采取以下步骤来创建一个Spring应用程序:
    1. 创建一个名称为SpringExample的项目,并在创建的项目的src文件夹下创建一个包com.jc2182
    2. 使用“添加外部JAR”选项添加所需的Spring库,如“Spring Hello World示例”一章中所述。
    3. 在com.jc2182包下创建Java类Student 和 MainApp。
    4. 在src文件夹下创建Beans配置文件Beans.xml。
    5. 最后一步是创建所有Java文件和Bean配置文件的内容,然后按以下说明运行应用程序。
    以下是Student.java内容。
    package com.jc2182;
    
    public class TextEditor {
       private SpellChecker spellChecker;
       
       // a setter method to inject the dependency.
       public void setSpellChecker(SpellChecker spellChecker) {
          System.out.println("Inside setSpellChecker." );
          this.spellChecker = spellChecker;
       }
       
       // a getter method to return spellChecker
       public SpellChecker getSpellChecker() {
          return spellChecker;
       }
       public void spellCheck() {
          spellChecker.checkSpelling();
       }
    }
    以下是MainApp.java文件的内容。
    package com.jc2182;
    
    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");
          
          HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
          objA.getMessage1();
          objA.getMessage2();
    
          HelloChina objB = (HelloChina) context.getBean("helloChina");
          objB.getMessage1();
          objB.getMessage2();
          objB.getMessage3();
       }
    }
    以下是配置文件的beans.xml它可以对基于setter方法注入配置
    <?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.jc2182.Student">
          <property name = "name" value = "Zara" />
    
          <!-- try without passing age and check the result -->
          <!-- property name = "age"  value = "11"-->
       </bean>
    
    </beans>
    完成创建源和Bean配置文件后,尝试运行, 它将包含以下错误消息:
    Error creating bean with name 'student' defined in class path resource [Beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.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.jc2182.Student">
          <property name = "name" value = "Zara" />
    
          <!-- try without passing age and check the result -->
          <property name = "age"  value = "11" />
       </bean>
    
    </beans>
    上面的示例将产生以下结果:
    Name : Zara
    Age : 11