Home > Annotations, Spring > Annotation-driven configuration and Auto-detection

Annotation-driven configuration and Auto-detection

June 3rd, 2010

For quiet some time now i have been working with annotation on the URS application. You may have see or used some common annotations already such the java based @Override annotation or the @Required spring annotation. In this post i’d like to highlight some of the annotation benefits i have come across and more importantly the annotation benefits that suit the UNE applications we are all developing.

Annotation-driven configuration was introduced in spring 2.5.x. Annotations can mark fields, methods and classes that need dependancy injection. This reduces configuration and dependancy mapping in spring configuration.

@Component, @Repository, @Service, and @Controller are four comnmonly known spring annotation. @Repository, @Service, and @Controller serve as specializations of @Component (generic stereotype). I have been using @Contoller for sometime and have found very useful, especially when combined with the benefits for spring 3’s REST support (more on that in a later post). Below is a controller with the annotation @Controller.

@Controller
public class TestController {
  // Fields, Methods etc
  @Autowired(required = true)
  public void setTestDao(TestDao testDao) {
    this.testDao = testDao;
  }
}

You can auto-detect @components by adding the following element in the spring-config.xml. The <component-scan/> element will auto-detect components for the base package of you application (filters can be applied for finer grained inclusion/exclusion of the package).

<?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-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:component-scan base-package="au.edu.une.example"/>

</beans>

This component-scan would automatically detect the controller class and register any corresponding BeanDefinitions with the ApplicationContext. Therefore there would be no need for the following in the spring configuration:

<bean name="testController" class="au.edu.une.example.TestController">
  <property name="testDao" value="testDao"/>
</bean>

Another handy annotation i have been making heavy use of is @Autowired (in example above). Its great for configuring controller dependencies on dao’s and other classes, when using auto detection. I have found @Autowired handy when writing unit tests too.

So try out the Annotation-driven configuration benefits in your application.

See Annotation-based configuration for more details on this Annotation-driven configuration via spring.

Categories: Annotations, Spring Tags:
Comments are closed.