Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Spring Annotations

There are two ways via which you can inject dependency in spring

  1. By configuring XML.
  2. By using annotation.d

Spring-Annotation is a library that enables the use or annotations to configure your application using spring-framework as a backend. In the previous tutorials, we learned how we can inject the dependencies from xml files. Spring also provides a mechanism where it can automatically handle the injection of properties and referred objects without defining them in xml files. This is accomplished by using Spring Annotations. Some of the important annotations that can come handy while working are:

  • @Required
  • @Autowired
  • @Qualifier

The @Required annotation applies to bean property setter methods. you can even register your own RequiredAnnotationBeanPostProcessor as a bean. the default implementation of the RequiredAnnotationBeanPostProcessor has a method called boolean shouldSkip(..) that checks for a boolean attribute named SKIP_REQUIRED_CHECK_ATTRIBUTE. The value of this attribute is checked for each bean during the post-processing by the RequiredAnnotationBeanPostProcessor. If it returns false, the @Required constraint is enforced, otherwise it is not.

The BeanPostProcessor that processes your @Required annotation defaults to RequiredAnnotationBeanPostProcessor, which is registered by default when you use context:annotation-config and context:component-scan in your configuration.

Example of Spring Required Annotation

Employee.java

package annotation;

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

public class Employee {
private int id;
private String name;
private Department dept;

public Department getDept() {
return dept;
}

@Required
public void setDept(Department dept) {
this .dept = dept;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

Department.java
package annotation;

public class Department {
String departmentName;

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this .departmentName = departmentName;
}
}

context.xml

........
........
<bean id="emp" class="annotation.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
<property name="dept" ref="dept"/>
</bean>

<bean id="dept" class="annotation.Department">
<property name="departmentName" value="Marketing"/>
</bean>

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

</beans>

Main.java

package annotation;

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

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext
("annotation/context.xml");
Employee emp = (Employee)context.getBean("emp");
System.out.println(emp.getName()+" "+emp.getDept().getDepartmentName());
}
}

The @Autowired annotation provides control over where and how autowiring can be done.This annotations can be done on setter method,contructor or property. We have seen each in detail with example, now we will see how to use autowire annotation.

The @Autowired annotation is performing Dependency Injection.

If @Autowired is applied to

  • a field: then the dependency is stored in this field
  • a setter: then the setter in invoked, with the parameter that is determined by the same algorithm like for the field dependency injection
  • a constructor: then the constructor in invoked with the parameters determined by the same algorithm like for the field dependency injection
Employee.java

package annotation;

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

public class Employee {
private int id;
private String name;
private Department dept;

public Department getDept() {
return dept;
}

@Autowire
public void setDept(Department dept) {
this .dept = dept;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

context.xml

........
........
<bean id="emp" class="annotation.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
</bean>

<bean id="dept" class="annotation.Department">
<property name="departmentName" value="Marketing"/>
</bean>

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

</beans>

Spring provides auto-wiring based on both XML and Annotations.
In this tutorial we will look into auto-wiring based on Annotations using Qualifiers to specify the bean name to be injected.

Auto-Wiring based on annotations is achieved as follows:

  • Use the @Autowired annotation to qualify either the member or a corresponding method (usually the setter method) which takes the injected type as argument
  • Use the @Qualifier(name="..") annotation to provide the name of the bean to be used by Spring for dependency injection

Using @Autowired annotation is similar to using XML auto-wire configuration using byType. The @Autowired annotation can be used to qualify:

  • a member
  • any method (including setter method) which takes the injected type as argument

If multiple bean types are available for injection, then Spring will be unable to make a decision on which bean to inject and will throw an Exception. In such cases, we can use the @Qualifier(name="..") annotation and give the name of the bean that we want Spring to inject. This is equivalent to auto-wiring by name using XML.

Note: If Spring finds no bean of injected type for auto-wiring, then it will throw NoSuchBeanDefinitionException. If in such a situation, we don't want Spring to throw the exception and it is acceptable to leave the dependency un-injected then use @Autowired(required=false).

Employee.java

package annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {
private int id;
private String name;
private Department dept;

public Department getDept() {
return dept;
}

@Autowire
@Qualifier("dept1")
public void setDept(Department dept) {
this .dept = dept;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

context.xml

........
........
<bean id="emp" class="annotation.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
</bean>

<bean id="dept1" class="annotation.Department">
<property name="departmentName" value="Marketing"/>
</bean>

<bean id="dept2" class="annotation.Department">
<qualifier value="employee"/>
<property name="departmentName" value="Marketing"/>
</bean>

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

</beans>

In context.xml we have registered predefined class org.springframework.beans.factory.annotation .AutowiredAnnotationBeanPostProcessor class for using @Autowired annotation, before we have used org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor class for using @Required annotation. Now if we do not want register this class in xml file, then we have to used <context:annotation-config> tag in xml file in place of these register class.

<context:annotation-config> is used to activate annotations in beans already registered in the application context (no matter if they were defined with XML or by package scanning).

context.xml

........
........
<bean id="emp" class="annotation.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
</bean>

<bean id="dept1" class="annotation.Department">
<property name="departmentName" value="Marketing"/>
</bean>

<bean id="dept2" class="annotation.Department">
<qualifier value="employee"/>
<property name="departmentName" value="Marketing"/>
</bean>

<context:annotation-config/>

</beans>


     << Previous
Next >>