Saturday, 4 May 2019

Explain @Required Annotation in Spring

@Required Annotation in Spring

Since Spring 2.5, annotation-based configuration has been an alternative to XML setups. Annotation based configuration rely on the bytecode metadata for wiring up components instead of angle-bracket declarations (Also Read : Introduction to Spring Boot). Annotations can be used on the relevant class, method, or field declaration.


Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches. In order to use annotation-based wiring, we need to enable it in our Spring configuration file. So we need to implicitly register annotations by including the following tag in an XML-based Spring configuration:

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">



  <i> <context:annotation-config/></i>



</beans>




Some of the important annotations are listed as below:

@Required: Introduced in Spring 2.0, the @Required annotation applies to bean property setter methods.

@Autowired: Introduced in Spring 2.5, the @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.

@Qualifier: The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.
JSR-250 Annotations: Introduced in Spring 2.5, it added support for JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations.
JSR-330 Annotations: Introduced in Spring 3.0, it added support for JSR-330 based annotations contained in the javax.inject package such as @Inject and @Named.

In this post I shall cover @Required annotation. Rest of the annotations shall be covered in my next posts.

@Required Annotation

As said above the @Required annotation applies to bean property setter methods and enforces required properties. Let’s see this in an example below. Before we look into the example let’s see RequiredAnnotationBeanPostProcessor enabling. @Required annotation can be registered by enabling RequiredAnnotationBeanPostProcessor in two ways:

1. Include in bean configuration file.Example:

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.2.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.2.xsd">



 <b>  <context:annotation-config/></b>



   

   <bean id="product" class="com.aid.requiredexample.Product">

      <property name="name"  value="ProductA" />



      

      

   <bean id="product" class="com.aid.requiredexample.Product">

      <property name="name"  value="ProductA" />



      

      

   <bean id="product" class="com.aid.requiredexample.Product">

      <property name="name"  value="ProductA" />



      

      

    <bean id="product" class="com.aid.requiredexample.Product">

       <property name="name"  value="ProductA" />



       

       <property name="price"  value="400"/>

    </bean>



</beans>


Now execute the code and the below output appears on the console:

Product Name : ProductA
Price : 400

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...