Spring Package Scanning for @Component Decorated POJOs
This little tip shows you how to do package scanning for @Component decorated POJOs with the AnnotationConfigWebApplicationContext.
Last week, TheServerSide.com published an 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.
Updated inversion of control (IoC) tutorials and dependency injection examples
TheServerSide has been updating its resources in terms of the Spring framework, Java EE contexts and various different approaches to inversion of control (IoC) and dependency injection (DI). Check out these newer resources on the topic:
- Inversion of control explained fast with a Spring IoC example
- Spring MVC Java web development with Spring Boot
- Drawbacks to IoC and how to solve inversion of control problems
- Spring IoC vs. Google Guice's inversion of control approach
- Getting started with Google Guice
- Getting started with the Spring IoC container
- The beauty of Spring without XML: Annotation based IoC
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="https://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
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