Master java skills

@Autowired Annotation Spring

@Autowired annotation is used for autowiring purpose in spring framework.

1. Autowiring by type using @Autowired

While using autowiring by type, we need to make sure that there is just one bean in spring container that needs to be wired.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sks</groupId>
  <artifactId>autowiring-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.3.0</version>
		</dependency>
	</dependencies>
	
</project>

Employee.java – Using @Autowired on top of property or setter method is equivalent to autowiring by type

package com.sks;

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

public class Employee {
	
	private int id;
	private String name;
	private int age;
	
	//This is autowiring by type
	@Autowired
	private Address add;
	
	//getters and setters
	
	public Address getAdd() {
		return add;
	}
	
	//@Autowired can be used here as well
	public void setAdd(Address add) {
		this.add = add;
	}
	public Employee(int id, String name, int age, Address add) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.add = add;
	}
	public Employee(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", add=" + add + "]";
	}

}

Address.java

package com.sks;

public class Address {

	private String addLine;
	private String city;
	private String country;
	private int pin;

	public String getAddLine() {
		return addLine;
	}

	public void setAddLine(String addLine) {
		this.addLine = addLine;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public int getPin() {
		return pin;
	}

	public void setPin(int pin) {
		this.pin = pin;
	}

	public Address(String addLine, String city, String country, int pin) {
		super();
		this.addLine = addLine;
		this.city = city;
		this.country = country;
		this.pin = pin;
	}

	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Addresss [addLine=" + addLine + ", city=" + city + ", country=" + country + ", pin=" + pin + "]";
	}

}

AppConfig.java – We need to make sure that we have only one bean of Address in the below class

package com.sks;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.sks")
public class AppConfig {
	
	@Bean("emp")
	public Employee getEmployee() {
		return new Employee(10, "Mark", 25);
	}
	
	@Bean
	public Address getAddress() {
		return new Address("101, street no 10", "Sydney", "Australia", 208128);
	}

}

Testing.java

package com.sks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Testing {
	
	public static void main(String[] args) {
		
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		
		Employee e = (Employee)context.getBean("emp");
		
		System.out.println(e);
		((AnnotationConfigApplicationContext)context).close();
		
	}
}

Output :

1. Autowiring by name using @Autowired

We need to use @Qualifier annotation to specify the name of the bean that we want to autowire. Make following changes in the above example

Employee.java

package com.sks;

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

public class Employee {
	
	private int id;
	private String name;
	private int age;
	
	//This is autowiring by name. It will try to autowire an Address bean with name add2
	@Autowired
	@Qualifier("add2")
	private Address add;
	
	//getters and setters
	
	public Address getAdd() {
		return add;
	}
	
	//@Autowired can be used here as well
	public void setAdd(Address add) {
		this.add = add;
	}
	public Employee(int id, String name, int age, Address add) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.add = add;
	}
	public Employee(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", add=" + add + "]";
	}

}

AppConfig.java

package com.sks;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.sks")
public class AppConfig {
	
	@Bean("emp")
	public Employee getEmployee() {
		return new Employee(10, "Mark", 25);
	}
	
	@Bean
	public Address getAddress() {
		return new Address("101, street no 10", "Sydney", "Australia", 208128);
	}
	
	@Bean("add2")
	public Address getAddressOne() {
		return new Address("102, street no 55", "Melbourne", "Australia", 768128);
	}
}

3. Autowiring using constructor

The first change to do is that comment out employee bean code in AppConfig class

AppConfig.java

package com.sks;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.sks")
public class AppConfig {
	
	/*
	 * @Bean("emp") public Employee getEmployee() { return new Employee(10, "Mark",
	 * 25); }
	 */
	
	@Bean("add")
	public Address getAddress() {
		return new Address("101, street no 10", "Sydney", "Australia", 208128);
	}
	
	@Bean("add2")
	public Address getAddressOne() {
		return new Address("102, street no 55", "Melbourne", "Australia", 768128);
	}

}

Employee.java

package com.sks;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component("emp")
public class Employee {
	
	private int id;
	private String name;
	private int age;
	
	private Address add;
	
	//getters and setters
	
	@Autowired
	//Use @value and @Qualifier for autowiring
	public Employee(@Value("11") int id, @Value("Mark") String name, @Value("30") int age, @Qualifier("add2") Address add) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.add = add;
	}
	
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", add=" + add + "]";
	}
	
}

Rest of the classes remain unchanged.

Output :