The Spring autowired annotation example shows you how to configure bean dependency injection using annotation. In the previous example, you can use XML to describe a bean wiring, and now you can use annotation to configure the bean wiring. In case, a bean wiring is configured both with annotation and XML, the XML will override the annotation configuration. Because Spring container will perform annotation injection before XML injection.
Let’s begin:
Application Data Access Object(DAO)
First of all, We create the Data Access Object(DAO) with a simple class HelloSpringDAO.
1 2 3 4 5 6 7 8 |
package com.javabycode.spring; public class HelloSpringDAO { public String getMsg() { return "Spring autowired annotation example"; } } |
Application Service and Injecting Bean using Spring annotation Configuration
Before we can use the spring annotation configuration we must enable it in our spring configuration file(spring-config.xml) by add this line
1 |
<context:annotation-config/> |
Then we use the @Autowired to inject a bean automatically. I notice that the @Autowired is used at method, constructor, or field level to mark the component must be automatically injected by Spring. Let’s see the below snippet code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode.spring; import org.springframework.beans.factory.annotation.Autowired; public class HelloSpringService { @Autowired private HelloSpringDAO dao; public void sayHello() { System.out.println(dao.getMsg()); } public void setDao(HelloSpringDAO springDAO) { this.dao = springDAO; } } |
Spring Configuration
Don’t forget enable Spring annotation configuration because it is not turned on by default as it is mentioned above by using
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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> Spring autowired annoation example. </description> <context:annotation-config /> <bean class="com.javabycode.spring.HelloSpringService"/> <bean class="com.javabycode.spring.HelloSpringDAO"/> </beans> |
Demo Spring application
In this Spring autowired annoation example, we create main class and load all the bean definitions using the constructor of the ApplicationContext. This context will bootstrap the Spring application.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"}); HelloSpringService service = context.getBean(HelloSpringService.class); service.sayHello(); } } |
Run the main class above, the console will print the following message.
1 |
Spring autowired annotation example |
That’s it on the Spring autowired annotation example.
References
Spring Annotation Based Configuration Doc
Download complete source code, click link below
spring-autowired-annotation-example.zip (267 downloads)