Monday 1 June 2015

Spring Required annotation

By using Required annotation, we can validate, whether bean is initialized with all required values or not. Required annotation applied on bean setter mehods and validates whether bean initialized properly with value or not. If bean is not initialized with required value, then exception thrown while bean being initialized.

Step 1 : Create new maven project “spring_tuorial”. Project structure looks like below.

Step 2 : Update “pom.xml” file for maven dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
 <modelVersion>4.0.0</modelVersion> 
 <groupId>spring_tutorial</groupId> 
 <artifactId>spring_tutorial</artifactId> 
 <version>0.0.1-SNAPSHOT</version> 
 <packaging>war</packaging> 
 <name>spring_tutorial</name> 
 <description>spring_tutorial</description> 
 <properties> 
  <org.springframework-version>4.1.5.RELEASE</org.springframework-version> 
 </properties> 

 <dependencies> 
  <dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-context</artifactId> 
   <version>${org.springframework-version}</version> 
  </dependency> 

 </dependencies> 
</project>

Step 3: Create new package “com.springtutorial.model” under “src/main/java”.


Step 4: Create “Employee” class under the package “com.springtutorial.model”.
package com.springtutorial.model; 

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

public class Employee { 
  private String firstName; 
  private String lastName; 
  private int id; 

  public String getFirstName() { 
    return firstName; 
  } 

  public void setFirstName(String firstName) { 
    this.firstName = firstName; 
  } 

  public String getLastName() { 
    return lastName; 
  } 

  public void setLastName(String lastName) { 
    this.lastName = lastName; 
  } 

  public int getId() { 
    return id; 
  } 

  @Required 
  public void setId(int id) { 
    this.id = id; 
  } 

  @Override 
  public String toString() { 
    return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", id=" + id + "]"; 
  } 

} 

Step 5: Create “spring.xml” file in “src/main/resources”.

spring.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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

 <bean id="employee" class="com.springtutorial.model.Employee"> 
  <property name="firstName" value="Hari Krishna" /> 
  <property name="lastName" value="Gurram" /> 
 </bean> 

 <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> 
</beans>

"spring.xml" is used to assign unique IDs to different beans, to control the creation of objects with different values. You have to make sure that “spring.xml” file is available in CLASSPATH and use the same name in main application while creating application context as shown in Main.java file.

Step 6 : Create package “com.springtutorial.main” under “src/main/java”.

Step 7 : Create Main class under package “com.springtutorial.main”.
package com.springtutorial.main; 

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

import com.springtutorial.model.Employee; 

public class Main { 
  public static void main(String args[]) { 
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 

    Employee emp = (Employee) context.getBean("employee"); 
    System.out.println(emp); 
    
    ((ClassPathXmlApplicationContext) context).close(); 
  } 
} 


Step 8: Run Main.java,you will get error like below, since id is not initialized for employee.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employee' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'id' is required for bean 'employee' 

Final project structure looks like below.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment