欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

Spring @Required 注释

Spring @Required 注释

@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注释的示例。

示例:

让我们使 Eclipse IDE 处于工作状态,请按照下列步骤创建一个 Spring 应用程序:

步骤 描述
1 创建一个名为SpringExample的项目,并且在所创建项目的src文件夹下创建一个名为com.tutorialspoint的包。
2 使用AddExternalJARs选项添加所需的Spring库文件,就如在SpringHelloWorldExample章节中解释的那样。
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

作者:w3cschool | 来源:https://www.w3cschool.cn

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 » Spring @Required 注释

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏