Tutorial:

Package Scanning for @Component Decorated POJOs with the AnnotationConfigWebApplicationContext

By Mark Spritzler & Cameron McKenzie

TheServerSide.com

Last week, TheServerSide.com published Jason Tee's article on the simplest, web based, non-Spring MVC application you could develop that could demonstrate how to leverage the facilities of the Spring IoC container using an annotation based configuration. However, as simple as this documented application is, there is a quick fix that would make it even simpler.

comment on this article  :  ask a question about getting this example to work

Lose the SimpleConfig Class

First, the application uses a class named SimpleConfig, which is decorated with the @Configuration annotation. We can actually eliminate this class in its entirety.

 

package com.mcnz.spring;

import org.springframework.context.annotation.*;

@Configuration
public class SimpleConfig {

  @Bean
  @Scope(value="session")
  public ClickCounter clickCounter() {
    return new ClickCounter();
  }
}

 

Annotate the POJO with @Component and @Scope Annotations

Rather than using the SimpleConfig class, we simply decorate the ClickCounter class instead, moving the @Scope annotation into the POJO, and adding in the @Component annotation:

package com.mcnz.spring;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value="session")
public class ClickCounter {
  private int count;
  public int getCount() {return count;}
    public void setCount(int count) {this.count = count;}
}

 

Configure the Deployment Descriptor for Package Level Component Scanning

With those changes made, you can simply list the names of the packages in which the @Component decorated classes reside using the contextConfigLocation context parameter entry in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.mcnz.spring</param-value>
  </context-param>

  <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <listener>
    <listener-class>
    org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
</web-app>

This little change further simplifies the application by eliminating the SimpleConfig class entirely, while allowing the Spring framework to search for @Component and @Configuration annotated classes in the specified package.

How to Use Spring 3.0 In a Servlet Based Web Application By Jason Tee

comment on this article  :  ask a question about getting this example to work

 

Recommended Books for Learning Spring

Spring in Action  ~ Craig Walls
Spring Recipes: A Problem-Solution Approach  ~ Gary Mak
Professional Java Development with the Spring Framework ~ Rod Johnson PhD
Pro Java EE Spring Patterns: Best Practices and Design Strategies ~ Dhrubojyoti Kayal


08 Nov 2010