Spring Boot Autowired Constructor

By Arvind Rai, September 23, 2023
On this page we will learn to use @Autowired annotation with constructor in our Spring Boot application.
1. The @Autowired annotation marks constructor, field, setter method, or config method to be autowired by Spring dependency injection. We can achieve the same by using JSR-330 Inject annotation.
2. The @Autowired annotation has required attribute with default value true. It decides whether the annotated dependency is required.
3. We can annotate only one constructor with @Autowired that has required value as true. The bean will be initialized using this constructor for dependency injection.
4. We can annotate more than one constructor with @Autowired using required value as false. In this case all these constructors are eligible for autowiring and Spring will choose that constructor for bean initialization that satisfies greatest number of dependencies. If none of constructor can be chosen then the default or primary constructor will be chosen, if available.
5. If a class has multiple constructors without annotating @Autowired, then Spring bean will be initialized with default/primary constructor.
6. If a class has single constructor, then bean will always be initialized with this constructor for dependency injection.
7. The constructors annotated with @Autowired need not to be public to initialize bean. It means both public and non-public constructors annotated with @Autowired can be used by Spring for bean initialization.

1. Single Constructor

Find a class with single constructor.
@Service
public class CustomerService {
	@Autowired
	public CustomerService(Customer customer) {
                  ------
	}
	------
} 
If the class has single constructor, it is not not necessary to autowire it. Spring will initialize bean with this constructor always whether @Autowired is used or not.

2. Multiple Constructors

Suppose our class has multiple constructors annotated with @Autowired. In this case autowiring must be used with @Autowired(required = false). This is because for the true value of required attribute, the class can have only one constructor. The default value of required is true.
In case of multiple autowired constructors, Spring will choose that constructor which satisfies greatest number of dependencies.
Find the sample example.
CustomerService.java
@Service
public class CustomerService {
	private Customer customer;
	private Address address;

	@Autowired(required = false)
	public CustomerService(Customer customer) {
		this.customer = customer;
	}

	@Autowired(required = false)
	public CustomerService(Customer customer, Address address) {
		this.customer = customer;
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}

	public Customer getCustomer() {
		return customer;
	}
} 
In the above class, both constructors are eligible for autowiring.
1. If both Customer and Address classes are annotated with @Component or other stereotypes then following constructor will be used to initialize this bean.
@Autowired(required = false)
public CustomerService(Customer customer, Address address) {
	this.customer = customer;
	this.address = address;
} 
2. If only Customer class is a component then following constructor will be used to initialize the bean.
@Autowired(required = false)
public CustomerService(Customer customer) {
	this.customer = customer;
} 

Find other files of example.
Customer.java
@Component
public class Customer {
	public String getMessage() {
		return "I am a happy customer.";
	}
} 
Address.java
@Component
public class Address {
	public String getCity() {
		return "Varanasi";
	}
} 
SpringBootAppStarter.java
@SpringBootApplication
public class SpringBootAppStarter {
	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootAppStarter.class, args);
		CustomerService service = ctx.getBean(CustomerService.class);
		System.out.println(service.getCustomer().getMessage());
		System.out.println(service.getAddress().getCity());
		ctx.close();
	}
} 
Find the output.
Spring Boot Autowired Constructor

3. Reference

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us