Spring 的@Required注释

本文介绍了Spring框架中@Required注解的使用方法及注意事项。通过一个具体的示例演示了如何在Java类的setter方法上使用该注解来确保在XML配置文件中必须设置相应的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例子源于:W3Cschool,在此做一个记录

@Required注释为为了保证所对应的属性必须被设置,@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注释的示例。直接的理解就是,如果你在某个java类的某个set方法上使用了该注释,那么该set方法对应的属性在xml配置文件中必须被设置,否则就会报错!!!

例子如下:

Studnent.java

package com.how2java.w3cschool.required;

import org.springframework.beans.factory.annotation.Required;

public class Student {
    private Integer age;
    private String name;

    public Integer getAge() {
        return age;
    }

    @Required     //该注释放在的是set方法中,如果没有在xml配置文件中配置相关的属性,就会报错
    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    @Required
    public void setName(String name) {
        this.name = name;
    }

}

 

MainApp.java

package com.how2java.w3cschool.required;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beanrequired.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("Name:" + student.getName());
        System.out.println("age:" + student.getAge());

    }

}

第一次的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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    <!-- student bean的定义 -->
    <bean id = "student" class="com.how2java.w3cschool.required.Student">
    <property name = "name" value="派大星"/>
    
    </bean>
    
</beans>

应该和注意到Student.java中的@Required注释应用在了两个set方法上,但是此时在xml的bean中只设置了一个属性,因此从报错,“ Property 'age' is required for bean 'student'

 

将对应的属性设置加上后的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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    <!-- student bean的定义 -->
    <bean id = "student" class="com.how2java.w3cschool.required.Student">
    <property name = "name" value="派大星"/>
    <property name = "age" value="5"/>
    </bean>
    
</beans>

最后的结果输出为:

 

转载于:https://www.cnblogs.com/Guhongying/p/10598732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值