In this post I will show you how to use spring 3.0 Java based configuration in a Java SE environment, taking advantage of spring autowiring to implement the chain of responsibility design pattern with components and services in a simple project. We will also use spring’s PropertySourcesPlaceholderConfigurer to inject custom properties into our beans with the @Value annotation and test our application with spring-test.

Some fun in perspective ! Tag along.

The source code of this application is available on my github :

Java configuration

Starting from spring 3.0, it is possible to get rid of any XML configuration by providing a pure Java configuration. This is done by annotating your configuration classes with the @Configuration annotation and annotating your beans with @Bean:

@ComponentScan(basePackages = {"com.geowarin.spring.service", "com.geowarin.spring.component"})
@PropertySource(value = "")
public class SpringStandalonChainConfig {

	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
		return pspc;

	public MainBean mainBean() {
		return new MainBean();

Additional annotation for java configuration include @ComponentScan to specify packages in which your @Component, @Service, etc. beans are included and @PropertySource to include property files in your configuration.

Two thing here :

  1. I declare a MainBean which will act as an entry point for our application. This bean will benefit of spring autowiring
  2. I declare a custom PropertySourcesPlaceholderConfigurer to enable the injection of properties annotated with @Value. I am customizing the prefix for usage of spring Expression Language to be able to use #{} expressions instead of default ${}

Injection with @Value property is a very interesting alternative to the use of spring’s Environment as it provides natural type inference.

For some people, the use of java configuration can be confusing because one cannot see at first glance where the config is located. What I usually do is keeping my configuration in a separate source folder. This can be achieved with maven and its build-helper plugin :

This approach is compatible with eclipse if you have m2e installed. In that case when you import a project using this plugin, eclipse will prompt you for the install of the build-helper connector.

Our application entry point : the MainBean

Here is the code of our MainBean :

public class MainBean {

	private ChainService service;

	@Value("#{chain.compatibleWithFirst}") boolean compatibleWithFirst;
	@Value("#{chain.compatibleWithSecond}") boolean compatibleWithSecond;

	private static Logger log = LoggerFactory.getLogger(MainBean.class);

	public void start() {"property compatibleWithFirst=" + compatibleWithFirst);"property compatibleWithSecond=" + compatibleWithSecond);

		ChainContext chainContext = new ChainContext(compatibleWithFirst, compatibleWithSecond);


We are injecting our service into the bean with a custom qualifier which will enable us to provide several implementations of our service if we need it.

Note that the @Value annotation, our properties will automatically be casted to booleans, which is pretty cool. Here is our file :


The Chain : Two components and a service

The ChainContext class is a simple pojo we pass to our service to be handled by the chain of responsibility and enable us to test if our service successfully handled our case :

public class ChainContext {

	private final boolean compatibleWithFirstElement;
	private final boolean compatibleWithSecondElement;

	private boolean handledByFirst;
	private boolean handledBySecond;

	public ChainContext(boolean compatibleWithFirstElement, boolean compatibleWithSecondElement) {
		this.compatibleWithFirstElement = compatibleWithFirstElement;
		this.compatibleWithSecondElement = compatibleWithSecondElement;

	// Getters and setters ommited

ChainElement is a simple interface which will be implemented by two components : FirstChainElement and SecondChainElement.

public interface ChainElement {

	public boolean doChain(ChainContext context);

Here is the first element, the second one is essentially the same thing :

public class FirstChainElement implements ChainElement {

	private static Logger log = LoggerFactory.getLogger(FirstChainElement.class);

	public boolean doChain(ChainContext context) {

		if (context.isCompatibleWithFirstElement()) {"Handled by first");
			return true;

		return false;

The thing to note here is the use of the spring annotation @Order which will enable us to sort our list using spring’s AnnotationAwareOrderComparator. Neat :)

And now the service :

public class DoChainService implements ChainService {

	private List<ChainElement> chain;

	public void init() {
		Collections.sort(chain, AnnotationAwareOrderComparator.INSTANCE);

	public void executeChain(ChainContext context) {

		for (ChainElement chainElement : chain) {
			if (chainElement.doChain(context))

Note that we use the same qualifier as our MainBean here. The main trick in this article is the usage of @Autowired to inject all the components implementing the ChainElement interface into a List.

The application main and tests

That’s it ! Now you can run your project with this main class :

public class SpringStandaloneChainApp {

	private static final String CONFIG_PACKAGE = "com.geowarin.spring.config";

	public static void main(String[] args) {

		try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {


			MainBean bean = ctx.getBean(MainBean.class);

And unit test the service with spring-test like that :

@ContextConfiguration(classes = { SpringStandalonChainConfig.class })
public class ChainServiceTest extends TestCase {

	private ChainService chainService;

	public void testHandledByFirst() {

		ChainContext chainContext = new ChainContext(true, false);


	public void testHandledBySecond() {

		ChainContext chainContext = new ChainContext(false, true);



Spring is perfectly suitable for a Java SE Environment. Its new java configuration is very handy and powerful as long as you keep things tidy and make sure the configuration is not spread across the whole project. Finally, let’s note it is possible to inject all components or services implementing a given interface into a list with the `@Autowired annotation.