@Required
Spring’s dependency checking in bean configuration file is used to make sure all properties of a certain types (primitive, collection or object) have been set. In most scenarios, you just need to make sure a particular property has been set, but not all properties.
For this case, you need @Required annotation, see following example:
@Required example:
A Customer object, apply @Required in setPerson() method to make sure the person property has been set.
| package com.param.common;import org.springframework.beans.factory.annotation.Required;public class Customer{ private Person person; private int type; private String action; public Person getPerson() { return person; } @Required public void setPerson(Person person) { this. person = person; }} |
Simply apply the @Required annotation will not enforce the property checking, you also need to register an RequiredAnnotationBeanPostProcessor to aware of the @Required annotation in bean configuration file.
The RequiredAnnotationBeanPostProcessor can be enabled in two ways:
1. Include <context: annotation-config />
Add Spring context and <context:annotation-config />
in bean configuration file.
| <context:annotation-config /> <bean id=”CustomerBean” class=”com.param.Customer”> <property name=”action” value=”buy” /> <property name=”type” value=”1″ /> </bean> <bean id=”PersonBean” class=”com.param.Person”> <property name=”name” value=”param” /> <property name=”address” value=”ABC” /> <property name=”age” value=”291″ /> </bean> |
2. Include RequiredAnnotationBeanPostProcessor
| <bean id =”id1″class=”org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor”/><bean id=”CustomerBean”class=”com.param.Customer”> <property name=”action” value=”buy” /> <property name=”type” value=”1″ /> </bean><bean id=”PersonBean”class=”com.param.Person”> <property name=”name” value=”param” /> <property name=”address” value=” ABC” /> <property name=”age” value=”291″ /></bean> |
If you run it , the following error message will be throw, because
person property is not set.
| org.springframework.beans.factory.BeanInitializationException:’person’ is required for bean ‘CustomerBean’ |
Conclusion:
Try @Required annotation, it is more flexible than dependency checking in XML file, because it can apply to a particular property only.
@restController:
1. Spring provides support for developing REST services. Spring, behind the scenes, uses HttpMessageConverters to convert the response into desired format [ JSON/XML/etc..] based on certain libraries available on the classpath and optionally, Accept Headers in request.
2. In order to serve JSON, we will be using Jackson library [jackson-databind.jar]. For XML, we will use Jackson XML extension [jackson-dataformat-xml.jar]. presence of these libraries in classpath will trigger Spring to convert the output in required format. Additionally, We will go a step further by annotating the domain class with JAXB annotations to support XML in case Jackson’s XML extension library is not available for some reason.
Note: If you are sending the request by just typing the URL in browser, you may add the suffix [.xml/.json] which help spring to determine the type of content to be served.
@Qualifire concept:
means, which bean is qualify to autowired on a field
| <beanclass=”org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/><bean id=”customer”class=”com.mkyong.common.Customer” /><bean id=”personA”class=”com.mkyong.common.Person” > <property name=”name” value=”A” /></bean> <bean id=”personB”class=”com.mkyong.common.Person” > <property name=”name” value=”B” /></bean></beans> |
| Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException:No unique bean of type [com..common.Person] is defined:expected single matching bean but found 2: [personA, personB] |
To fix above problem, you need @Qualifier to tell Spring about which bean should autowired.
| @Autowired @Qualifier(“personA”) private Person person; |
@Required-style
The @Required annotation is used to make sure a particular property has been set. If you are doing migration of your existing project to Spring framework or you have @Required-style annotation for whatever reasons, Spring is allowing you to define your custom @Required-style annotation, which is equivalent to @Required annotation.
Let’s See through Example:
We are going to create custom @Required-style annotation named @CustomRequired which is equivalent to @Required annotation.
Create the @CustomRequired interface
| com.param.common;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface CustomRequired {} |
Apply it to a property
| public class Customer{private Person person; private int type; private String action; @CustomRequired public void setPerson(Person person) { this.person = person; } //getter and setter methods} |
Register it
Include your new @CustomRequired annotation in ‘RequiredAnnotationBeanPostProcessor’ class.
| <beansxmlns=http://www.springframework.org/schema/beansxmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation=”http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd”><beanclass=”org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor”><propertyname=”requiredAnnotationType”value=”com.param.common.CustomRequired “/></bean><bean id=”CustomerBean”class=”com.param.common.Customer”><property name=”action” value=”sell” /><property name=”type” value=”1″ /></bean></beans> |
@ModelAttribute(“A”)
@component
@controller
@service
@transactional
@autowired
@ExceptionHandler
@lookup
@RequestMapping
@Resource