Spring: Working with Application Context.
?? Saral Saxena ??????
?11K+ Followers | Linkedin Top Voice || Associate Director || 14+ Years in Java, Microservices, Kafka, Spring Boot, Cloud Technologies (AWS, GCP) | Agile , K8s ,DevOps & CI/CD Expert
The below content explains how to work with the Spring Application Context, focusing on adding beans to the context using different methods such as @Bean annotation, stereotype annotations, and programmatically.
Spring: Working with Application Context.
We use the AnnotationConfigApplicationContext class to create the Spring context instance. Spring offers multiple implementations. Because in most cases we’ll use the AnnotationConfigApplicationContextclass.
We can add beans in the context in the following ways
Lets look at the main class and also a bean that we want to add to the Spring Context.
public class Main {
public static void main(String[] args) {
var context =
new AnnotationConfigApplicationContext();
Parrot p = new Parrot();
}
}
public class Parrot {
private String name;
// Omitted getters and setters
}
Adding Maven dependencies:
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sq-ch2-ex1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
STEP 1: DEFINING A CONFIGURATION CLASS IN YOUR PROJECT
The first step is to create a configuration class in the project. A Spring configuration class is characterized by the fact that it is annotated with the @Configuration annotation.
@Configuration
public class ProjectConfig {
}
STEP 2: CREATE A METHOD THAT RETURNS THE BEAN, AND ANNOTATE THE METHOD WITH @BEAN
One of the things you We do with a configuration class is add beans to the Spring context.
To do this, we need to define a method that returns the object instance we wish to add to the context and annotate that method with the @Bean annotation, which lets Spring know that it needs to call this method when it initializes its context and adds the returned value to the context.
领英推荐
@Configuration
public class ProjectConfig {
@Bean
Parrot parrot() {
var p = new Parrot();
p.setName("Koko");
return p;
}
}
STEP 3: MAKE SPRING INITIALIZE ITS CONTEXT USING THE NEWLY CREATED CONFIGURATION CLASS
We’ve implemented a configuration class in which we tell Spring the object instance that needs to become a bean.
Now we need to make sure Spring uses this configuration class when initializing its context. In below listing we will retrieve the Parrot class from the context.
public class Main {
public static void main(String[] args) {
var context =
new AnnotationConfigApplicationContext(
ProjectConfig.class);
}
}
public class Main {
public static void main(String[] args) {
var context =
new AnnotationConfigApplicationContext(
ProjectConfig.class);
Parrot p = context.getBean(Parrot.class);
System.out.println(p.getName());
}
}
Resolving Ambiguity:
Let’s see what happens when we define multiple beans in the Spring Context.
@Configuration
public class ProjectConfig {
@Bean
Parrot parrot1() {
var p = new Parrot();
p.setName("Koko");
return p;
}
@Bean
Parrot parrot2() {
var p = new Parrot();
p.setName("Miki");
return p;
}
@Bean
Parrot parrot3() {
var p = new Parrot();
p.setName("Riki");
return p;
}
}
We can’t get the beans from the context anymore by only specifying the type. If you do, you’ll get an exception because Spring cannot guess which instance you’ve declared you refer to. Look at the following listing. Running such a code throws an exception in which Spring tells you that you need to be precise, which is the instance you want to use.
public class Main {
public static void main(String[] args) {
var context = new
AnnotationConfigApplicationContext(ProjectConfig.class);
Parrot p = context.getBean(Parrot.class);
System.out.println(p.getName());
}
}
To solve this ambiguity problem, you need to refer precisely to one of the instances by using the bean’s name.
By default, Spring uses the names of the methods annotated with @Bean as the beans’ names themselves. Remember that’s why we don’t name the @Bean methods using verbs. In our case, the beans have the names parrot1, parrot2, and parrot3 (remember, the method represents the bean).
We can solve this problem by specifying the bean name.
public class Main {
public static void main(String[] args) {
var context = new
AnnotationConfigApplicationContext(ProjectConfig.class);
Parrot p = context.getBean("parrot2", Parrot.class);
System.out.println(p.getName());
}
}