Spring Annotation Configuration Example

This tutorial shows spring annotation configuration. Starting from Spring version 2.5, it is possible to configure bean dependency injection using annotations. So instead of using XML to describe a bean wiring, we can now use annotations to configure the bean wiring.

Info: Annotation injection is performed before XML injection. This means if a bean wiring is configured both with annotations and XML, the XML will override the annotation configuration.

Creating a Service

First we create a simple service, which we’ll use in another bean to wire to that component.

package com.memorynotfound.spring;

public class Emperor {

    public void kill(String name) {
        System.out.println("Killing " + name);
    }
}

Injecting Bean with Spring annotation Configuration

We can use the @Autowired at method, constructor or field level to indicate that the component must be automatically injected by spring. This autowiring is not enabled by default, so before we can use the spring annotation configuration we will need to enable it in our spring configuration file.

package com.memorynotfound.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class StarWars {

    @Autowired
    private Emperor emperor;

    public void kill(String beans) {
        emperor.kill(beans);
    }

    public void setEmperor(Emperor emperor) {
        this.emperor = emperor;
    }
}

Enabling Spring Annotation Configuration

Because spring annotation configuration is not turned on by default we need to enable it. We do this by registering the <context:annotation-config/> element in the spring configuration file. This will enable annotation driven configuration. Next we define our 2 bean definitions so we can wire them together, like we did in the previous StarWars class.

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

    <description>
        This tutorial shows spring annotation configuration. 
    </description>

    <context:annotation-config />

    <bean class="com.memorynotfound.spring.StarWars"/>
    <bean class="com.memorynotfound.spring.Emperor"/>

</beans>

Running the application

We bootstrap the spring application using the ClassPathXmlApplicationContext and providing the Resource location of the spring configuration file.

package com.memorynotfound.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Run {

    public static void main(String... args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"app-config.xml"});
        StarWars starWars = context.getBean(StarWars.class);
        starWars.kill("Luke Skywalker");
    }
}

Demo

After running the application, it’ll produce the following output.

Killing Luke Skywalker

References

Download

You may also like...