Since Spring 2.5, one of the most interesting ways of wiring beans in Spring has been to use annotations to automatically wire bean properties. Auto wiring with annotations isn’t much different than using the autowire attribute in XML. But it does allow for more fine-grained auto wiring, where you can selectively annotate certain properties for autowiring.
Annotation wiring isn’t turned on in the Spring container by default. So, before we can use annotation-based autowiring, we’ll need to enable it in our spring configuration. The simplest way to do that is with annotation element from the context Spring’ configuration namespace.
<context:annotation-config/>
Adding the above line would enable the autowiring mechanism in the spring framework. Alternatively, you can include the AutowiredAnnotationBeanPostProcessor to enable the autowiring feature as follows:
<bean class="org.springframework.beans.factory. annotation.AutowiredAnnotationBeanPostProcessor"/>
The following is the simplest way to enable:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi=http://www.w3.org/XMLSchema-instance xmlns:context=http://www.springframework.org/schema/context xsi:schmeaLocation=" 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/schmea/context/spring-context-3.0.xsd”> <context:annotation-config/> <context:component-scan base-package="com.java.beat.beans"/> </beans>
tells the Spring that you intend to use annotation-based wiring in Spring. Once it’s in place you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors.
also read:
- Spring interview questions
- Introduction to Spring framework
elements works by scanning a package and all of its sub-packages, looking for classes that could be automatically registered as beans in the Spring container. The base-package attributes tells the package to start its scan from. @Component annotation used to register classes as beans in a Spring container.
Spring 3.0 support a few different annotations for autowiring.
Spring’s own @Autowired annotation.
- The @Inject annotation from JSR-330.
- The @Resource annotation from JSR-250.
We’ll look at how to use @Autowired first. Then we’ll cover the remaining annotations in other posts.
@Autowired
Marked a bean property with @Autowired will surely push Spring framework to wire it with the targeted one using by Type. What’s especially interesting about @Autowired is that you don’t have to use it with a property declaration or a setter method. You can use it on any method to automatically wire in bean references.
package com.java.beat.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component("holderBean") public class HolderBean { @Autowired @Qualifier("wiredBean") private WiredBean wiredBean; public WiredBean getWiredBean() { return wiredBean; } public void setWiredBean(WiredBean wiredBean) { this.wiredBean = wiredBean; } }
package com.java.beat.beans; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component("wiredBean") @Qualifier("wiredBean") public class WiredBean { }
As you can see in the previous example, HolderBean has a reference to WiredBean, autowired using @Autowired annotation. As you can see, @Autowired won’t even thwarted by the private keyword. Even though the wiredBean property is private, it will still be autowired. Is there no limit to @Autowired ’s reach ?
Actually, there are a couple of circumstances that could keep @Autowired from getting its job done. Specifically, there must be exactly one bean that’s applicable for autowiring into the @Autowired property or parameter. If there are no applicable beans or if multiple beans could be autowired, then @Autowired will run into some trouble. Fortunately, there’s a way can we help @Autowired out in those circumstances.
First, let’s look at how to keep @Autowired from failing when there is not a matching bean.
Optional Autowiring
By default, @Autowired has a strong contract, requiring that the thing it annotates is wired. If no bean can be wired into the @Autowired – annotated property or argument, then autowired fails (with a nasty NoSuchBeanDefinitionException). That may be what you want – to have Spring fail early when autowiring goes bad rather than later with NullPointerException.
But it’s also possible that the property being wired is truly optional and a null value is acceptable. In that case, you can configure optional autowiring be setting @Autowired’s required attribute to false.
@Autowired(required=false) Private WiredBean wiredBean;
Qualifying Ambiguous Dependencies
In the other hand may be the problem’s not a lack of beans for Spring autowiring to choose from. May be it’s an abundance of (or at least two) beans, each of which is qualified to be wired into a property or parameter.
For example, suppose you have two beans that of type WiredBean. In that event, there is no way for @Autowired to choose which one you really want. So instead of guessing, a NoSuchBeanDefinitionException will be thrown and wiring will fail.
To help @Autowired figure out which bean you want, you can accompany it with Spring’s @Qualifier annotation.
@Autowired @Qualifier("wiredBean") private WiredBean wiredBean;