In this article, we will see a few important ways to create and inject beans using spring annotations.
Let's check this out using an example
Client is the client class, which is loading application context by registering DemoConfig class (@ComponentScan annotation) and getting Vehicle bean using context.
package sboot.basicannotations;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Client {
public static void main(String args[]) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(DemoConfig.class);
ctx.refresh();
Vehicle myVehicle = ctx.getBean("vehicle", Vehicle.class);
System.out.println("Context created...start creating a Vehicle now");
myVehicle.testVehicle();
ctx.close();
}
}
Class DemoConfig defines a few other beans using @Bean annotation which are required for Vehicle creation.
package sboot.basicannotations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class DemoConfig {
@Bean
public Engine getEngine() {
return new Engine();
}
@Bean
public Body getBody() {
Body body = new Body();
body.setSeats(getSeats());
body.setColor(getColor());
return body;
}
@Bean
public Seats getSeats() {
return new Seats();
}
@Bean
public Color getColor() {
return new Grey();
}
}
The output of the code is
Vehicle bean is instantiated using application context and @Component annotation
Engine is instantiated using @Bean annotation and injected into Vehicle using constructor injection
Body is instantiated using @Bean annotation and injected into Vehicle using @Autowired annotation
Seats is instantiated using @Bean annotation and injected into Body using setter injection
Color is an interface instantiated using @Bean annotation, which is implemented by class Grey
Color (Grey) is injected into Body using setter injection
Vehicle created successfully...
package sboot.basicannotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Vehicle {
private Engine engine;
//Constructor injection
public Vehicle(Engine engine) {
this.engine = engine;
}
@Autowired
private Body body;//@Autowired for injecting Body
public void testVehicle() {
engine.addEngine();
body.addBody();
//Seats & Color will be added in the Body
System.out.println("Vehicle created successfully...");
}
}
package sboot.basicannotations;
public class Engine {
public void addEngine() {
System.out.println("Engine is instantiated using @Bean annotation and injected into Vehicle using constructor injection");
}
}
package sboot.basicannotations;
public class Body {
private Seats seats;
//setter injection
public void setSeats(Seats seats) {
this.seats = seats;
}
private Color color;
//setter injection
public void setColor(Color color) {
this.color = color;
}
public void addBody() {
System.out.println("Body is instantiated using @Bean annotation and injected into Vehicle using @Autowired annotation");
seats.addSeats(4);
System.out.println("Color is an interface instantiated using @Bean annotation, which is implemented by class Grey");
color.addColor();
}
}
package sboot.basicannotations;
public class Seats {
public void addSeats(int noOfSeats) {
System.out.println("Seats is instantiated using @Bean annotation and injected into Body using setter injection");
}
}
package sboot.basicannotations;
public interface Color {
public void addColor();
}
package sboot.basicannotations;
public class Grey implements Color {
@Override
public void addColor() {
System.out.println("Color (Grey) is injected into Body using setter injection");
}
}
No comments:
Post a Comment