Spring @Autowired
annotation is used to automatically inject dependencies.
Where to apply @Autowired annotation
- You can apply the @Autowired annotation to setter methods. See example.
- You can apply the @Autowired annotation to constructors. See example.
- You can apply @Autowired to fields. See example.
- You can also apply the annotation to methods with arbitrary names and multiple arguments. See example.
Enabling @Autowired annotation
To enable autowiring using @Autowired annotation ‘AutowiredAnnotationBeanPostProcessor’ class has to be registered. You can provide bean definition for it directly, though that is seldom needed.
<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
By using <context:annotation-config>
element or using <context:component-scan>
element (which implicitly enables the functionality of <context:annotation-config> element) AutowiredAnnotationBeanPostProcessor class is registered automatically. This is the preferred way to enable it.
Spring @Autowired annotation example
In the example there is a class to place order called OrderService
and purchase can be done from a Store
. In OrderService class dependency for store has to be autowired. Using these classes we’ll see how to use @Autowired with constructor,
setter, field or arbitrary method.
Using @Autowired annotation on setter
When @Autowired annotation is applied on a setter method it is equivalent to autowiring = "byType" in case of XML configuration.
public interface IStore { public void doPurchase(); }
import org.springframework.stereotype.Service; @Service public class RetailStore implements IStore { public void doPurchase() { System.out.println("Doing purchase from Retail Store"); } }
public interface OrderService { public void buyItems(); }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class OrderServiceImpl implements OrderService { private IStore store; // Autowired on Setter @Autowired public void setStore(IStore store) { this.store = store; } public void buyItems() { store.doPurchase(); } }
Here you can see that the dependency on Store is satisfied by using @Autowired annotation on the setStore() method.
XML Configuration<?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"> <context:component-scan base-package="com.knpcode.springproject.service" /> </beans>
Using @Autowired annotation on constructor
@Autowired annotation on a bean’s constructor is equivalent to autowiring="constructor" in case of XML configuration.
@Service public class OrderServiceImpl implements OrderService { private IStore store; // Autowired on constructor @Autowired public OrderServiceImpl(IStore store){ this.store = store; } public void buyItems() { store.doPurchase(); } }
Using @Autowired annotation on field
@Autowired annotation on a field is equivalent to autowiring="byType" in autowiring using configuration file.
@Service public class OrderServiceImpl implements OrderService { // Autowiring on a field @Autowired private IStore store; public void buyItems() { store.doPurchase(); } }
Using @Autowired annotation on arbitrary methods
You can also apply the annotation to methods with arbitrary names and multiple arguments. Each of those arguments will be autowired with a matching bean in the Spring container.
@Service public class OrderServiceImpl implements OrderService { private IStore store; // Autowiring on a method @Autowired public void prepare(IStore store) { this.store = store; } public void buyItems() { store.doPurchase(); } }
That's all for the topic Spring @Autowired Annotation. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Spring @PostConstruct and @PreDestroy Annotation
- Spring Autowiring Example Using XML Configuration
- Exclude Bean From Autowiring in Spring
- Spring Boot and Dependency Injection
- How to Create Bootable USB Drive For Installing Ubuntu
- Is Java String Thread Safe
- Input Split in Hadoop MapReduce
- Abstraction in Python With Examples
No comments:
Post a Comment