Spring @Required注释

时间:2020-01-09 10:44:35  来源:igfitidea点击:

Spring @Required注释与一种方法(通常是setter方法)一起使用,从而使该属性的setter依赖项注入成为必需。

RequiredAnnotationBeanPostProcessor类

要使用@Required批注,需要注册RequiredAnnotationBeanPostProcessor类。默认的RequiredAnnotationBeanPostProcessor将通过" context:annotation-config"和" context:component-scan" XML标签进行注册。

Spring @Required注释示例

在示例中,有两个类Person和Address。

地址.java

public class Address {

	private String houseNo;
	private String street;
	private String city;
	private String state;
	private String pinCode;
	
	public String getHouseNo() {
		return houseNo;
	}
	public String getStreet() {
		return street;
	}
	public String getCity() {
		return city;
	}
	public String getState() {
		return state;
	}
	public String getPinCode() {
		return pinCode;
	}
	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public void setState(String state) {
		this.state = state;
	}
	public void setPinCode(String pinCode) {
		this.pinCode = pinCode;
	}
}

人.java

public class Person {

	private String name;
	private int age;
	private Address address;
	
	public String getName() {
		return name;
	}
	@Required
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Required
	public void setAddress(Address address) {
		this.address = address;
	}
	public Address getAddress() {
		return address;
	}
}

在Person类中,字段名称和地址引用使用@Required注释进行注释。这意味着这些值是必需的,否则将引发BeanCreationException。

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.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- Takes care of registering RequiredAnnotationBeanPostProcessor  -->
    <context:annotation-config/>

    <bean id="person" class="com.theitroad.springexample.Person">
        <!-- <property name="name" value="John"></property> -->  
        <property name="age" value="35"></property> 
    </bean>
    
    <bean id="address" class="com.theitroad.springexample.Address">
        <property name="houseNo" value = "432" />
        <property name="street" value = "E Lafayette Street" />
        <property name="city" value = "Detroit" />
        <property name="state" value = "Michigan" />
        <property name="pinCode" value = "48207" />
    </bean>
</beans>

在此请注意,尽管两个字段都使用@Required注释进行了注释,但并未提供name属性作为地址引用。

现在,如果我们运行此示例,

public class App {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
		Person person = context.getBean("person", Person.class);
		System.out.println("City- " + person.getAddress().getCity());

		context.close();
	}
}
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'person' defined in class path resource [appcontext.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'address' and 'name' are required for bean 'person'

如我们所见,Spring容器无法创建bean"个人",因为必需有操作"地址"和"名称",而没有提供这些属性的值。

在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.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- Takes care of registering RequiredAnnotationBeanPostProcessor  -->
    <context:annotation-config/>

    <bean id="person" class="com.theitroad.springexample.Person">
        <property name="name" value="John"></property>
        <property name="age" value="35"></property> 
        <property name="address" ref="address" />
    </bean>
    
    <bean id="address" class="com.theitroad.springexample.Address">
        <property name="houseNo" value = "432" />
        <property name="street" value = "E Lafayette Street" />
        <property name="city" value = "Detroit" />
        <property name="state" value = "Michigan" />
        <property name="pinCode" value = "48207" />
    </bean>
</beans>

现在显示正确的输出

City- Detroit

请注意,自5.1起,不推荐使用Spring @Required注释,而赞成对必需的设置使用构造函数注入。