zoukankan      html  css  js  c++  java
  • @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

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

    java

    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;
       }
    }

    会抛出异常的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>

    不会抛出异常的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" />
          <property name="age"  value="11"/>
       </bean>
    
    </beans>
  • 相关阅读:
    线性筛法(欧拉筛法)求素数
    07 day 2
    07 DAY 1
    二模 06day2
    刷水题记(2)
    The Perfect Stall (incomplete)
    离散化的应用:矩形覆盖问题
    刷水题记(1)
    发个题目坑 二模03day1
    hdu 5996 dingyeye loves stone(博弈)
  • 原文地址:https://www.cnblogs.com/wangdapeng/p/5053501.html
Copyright © 2011-2022 走看看