Created by Wang, Jerry on Jul 27, 2016
@Required
public void setChineseAddressService(final AddressService chineseAddressService)
{
this.chineseAddressService = chineseAddressService;
}
Copy the code
The @required annotation checks but it only checks that the property is set and does not test that the property is not empty
First we need to annotate the program
@Required
public void setProduct(Product product) {
this.product = product; } note@RequiredIt can only be set on setter methods and then we need to add XML <bean to our configuration fileclass="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
Copy the code
This is a Spring processor for checking that if Spring is 2.5 or above, we can configure it directly in the following way
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
<span style="color:#ff0000;">xmlns:context="http://www.springframework.org/schema/context"</span>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
<span style="color:#ff0000;">http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd</span>">
<span style="color:#ff0000;"><context:annotation-config/></span>
Copy the code
This configuration is complete, so if anything with a @ the Required attribute is not set Will be thrown BeanInitializationException anomalies