Followers

springIOC : @Required Annotation

Spring @required Annotation

In this tutorial you will see about spring @required annotation with an example. The @Required when written on top of setName() method it make sure that name property must have been set else it will give compile time error message that org.springframework.beans.factory.BeanInitializationException: Property 'name' is required for bean 'Employee' which is shown in this example.

Simply apply the @Required annotation will not enforce the property checking, you have to register an RequiredAnnotationBeanPostProcessor to aware of the @Required annotation in Bean configuration file. Simply applying @Required annotion is not enough to enforce the property checking, you also have to register RequiredAnnotationBeanPostProcessor in bean configuration file to do so. This configuration is done in two ways one by Include <context:annotation-config /> and another way is Include RequiredAnnotationBeanPostProcessor in bean configuration file. which is shown in this example's Beans.xml file.

Note: @required can be applied for dependent object also.

Employee.java
===========

package myspringIOC;

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

public class Employee {
    private int id;
    private String name;
    public Employee(){
       System.out.println("Employee constructor...");
    }

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

    public int getId() {
        return id;
    }
    @Required
    public void setId(int id) {
        this.id = id;
    }
  
   public String toString(){
   return id+" "+name;
   }
   
}

Beans.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-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- One way  -->
    <!-- <context:annotation-config /> -->
    <!-- Another way  -->
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    <bean id="employee" class="myspringIOC.Employee" >
         <property name="id" value="111" > </property>
     <!-- -->  <property name="name" value="I am an Indian" > </property>
    </bean>
</beans>

Test.java
=======
package myspringIOC;

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

public class Test {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  
          Employee emp = (Employee)context.getBean("employee");
          System.out.println(emp);
    }

}

No comments:

Post a Comment