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>
  • 相关阅读:
    腾讯课堂——基础数据类型(dict字典)
    腾讯课堂——基础数据类型(tuple元祖)
    基础数据类型(list列表)
    第 018讲:函数:灵活即强大(关键字函数,默认函数,收集函数)
    第 015讲:字符串:格式化
    第 013讲: 元组tuple 上了枷锁的列表
    第 012讲:打了激素的数组3
    第 011讲:一个打了激素的数组[02]
    range函数的用法
    第 010讲:一个打了激素的数组[01]
  • 原文地址:https://www.cnblogs.com/wangdapeng/p/5053501.html
Copyright © 2011-2022 走看看