Hello all,
In a recent project we began using the @Autowired spring annotation in our objects and services to help simplify and minimize the amount of configuration we were writing in our various xml files.
Introduced in Spring 2.5 annotation-based configurations are now a viable alternative to pure XML configuration. While it is debatable if one approach is “better” than the other, in practice it seems that mixing both methods allows you to spend less time wiring everything up and just getting on with it.
There are a number of annotations available including support for JSR-250 and JSR-330 but for this article I will be focusing purely on the @Autowired annotation. For more information please refer to the Spring documentation or any number of articles on the web.
What’s the point?
How many times have you had to explicitly wire your spring beans and services hunting for the proper bean names? What about creating setter methods for your private objects? What if there were an easier way to do it?
Spring 2.5 (and beyond) has introduced a new xml context schema that deals with ApplicationContext configuration related to plumbing – that is, not usually beans that are important to an end-user but rather beans that do a lot of the grunt work in Spring, such as BeanFactoryPostProcessors. To enable the tags in the context namespace, simply add the context namespace to your schema definition:
<?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-3.0.xsd">
<!-- <bean/> definitions here -->
</beans>
Now let’s look at a class as an example to see how @Autowired can help you in your daily development.
package emptest;
public interface EmployeeService {
String hire(String name);
String fire(String name);
}
package emptest.impl;
@Service
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDao employeeDao;
public String hire(String name) {
String message = employeeDao.getMessage("Hire");
return name + ", " + message;
}
public String fire(String name) {
String message = employeeDao.getMessage("Fire");
return name + ", " + message;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
}
In this simple example we have a service implementing an interface with a private DAO object. In a typical spring configuration for this service typically you would define your bean as:
<bean id="employeeService" class="emptest.impl.EmployeeServiceImpl">
<property name="employeeDao">
<ref bean=”employeeDao”/>
</property>
</bean>
But Spring can automatically detect classes and register bean definitions with the ApplicationContext! Let’s see how this works in practice.
Step 1. Add <context:component-scan base-package=”emptest.impl”/> to your configuration file. This tells spring to scan everything in the emptest.impl package for Spring stereotypes and annotations.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="emptest.impl"/>
</beans>
Step 2. Add @Autowired annotation to any private objects that you would like Spring to wire up for you.
package emptest.impl;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public String hire(String name) {
String message = employeeDao.getMessage("Hire");
return name + ", " + message;
}
public String fire(String name) {
String message = employeeDao.getMessage("Fire");
return name + ", " + message;
}
}
That’s it! Notice that there is no need to explicitly define a setter method, nor do you need to explicitly define your bean within the Spring configuration.
Of course this is a very basic scenario, but for the most part will get you started using annotation based configuration. There are lots of tutorials, guides and documentation available online for more advanced usages.
Cheers!