Spring annotations
Let’s do the previous example using annotations.
@Bean, @Configuration, and @ComponentScan
Add below maven project dependency
<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>spring-core-annotations-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>5.3.0</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Employee class
package com.sks;
import org.springframework.beans.factory.annotation.Value;
public class Employee {
private int id;
private String name;
private int age;
private boolean contract;
public Employee(int id, String name, int age, boolean contract) {
super();
this.id = id;
this.name = name;
this.age = age;
this.contract = contract;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", contract=" + contract + "]";
}
}
Create configuration file
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(name = "emp")
Employee getEmployee() {
return new Employee();
}
@Bean(name = "emp1")
Employee getEmployeeOne() {
return new Employee(10,"Joe",25,true);
}
}
Testing main class
package com.sks;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class EmployeeMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Employee emp = (Employee) context.getBean("emp");
Employee emp1 = (Employee) context.getBean("emp1");
System.out.println("emp : " + emp);
System.out.println("emp1 : " + emp1);
context.close();
}
}
Output :
emp : Employee [id=0, name=null, age=0, contract=false]
emp1 : Employee [id=10, name=Joe, age=25, contract=true]
@Value
This is used to give default values to the properties
Examples
@Value("100")
private int id;
@Value("Mark")
private String name;
@Value("30")
private int age;
@Value("true")
private boolean contract;
@Autowired
If there is a dependency of a custom class, such as Address class dependency in Employee class, we use @Autowired annotation.
Employee Class
package com.sks;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
private int id;
private String name;
private int age;
private boolean contract;
@Autowired
private Address add;
public Employee(int id, String name, int age, boolean contract) {
super();
this.id = id;
this.name = name;
this.age = age;
this.contract = contract;
}
public Employee() {
super();
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", contract=" + contract + ", add=" + add
+ "]";
}
}
Address Class
package com.sks;
public class Address {
private String addLine;
private String city;
public Address(String addLine, String city) {
super();
this.addLine = addLine;
this.city = city;
}
public Address() {
super();
}
@Override
public String toString() {
return "Address [addLine=" + addLine + ", city=" + city + "]";
}
}
Define a bean of Address class in AppConfig java 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(name = "emp")
Employee getEmployee() {
return new Employee();
}
@Bean(name = "emp1")
Employee getEmployeeOne() {
return new Employee(10,"Joe",25,true);
}
@Bean(name = "add")
Address getAddress() {
return new Address("Street No 1","London");
}
}
@Scope
@Scope is used to define the scope of a bean.
@Bean(name = "emp")
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
Employee getEmployee() {
return new Employee();
}