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>
  • 相关阅读:
    第六章 装饰模式
    第二章 策略模式
    第一章 简单工厂模式
    HTTPS-post请求
    import&export
    Flask(Jinja2) 服务端模板注入漏洞vulhub
    MySQL UDF提权 过程及注意事项
    centos7 安装jdk1.8.0_271 以及错误解决
    WEB、FTP服务器所有响应码解释(超详细)
    Wolf CMS后台文件上传getshell并提权
  • 原文地址:https://www.cnblogs.com/wangdapeng/p/5053501.html
Copyright © 2011-2022 走看看