There are two ways via which you can inject dependency in spring
- By configuring XML.
- 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:
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
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
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
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).
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).