Java-based configuration in Spring allows you to configure your Spring application using Java classes annotated with @Configuration
. In Java based configuration you use the AnnotationConfigApplicationContext
class to load your configuration and retrieve beans.
ApplicationContext context = new AnnotationConfigApplicationContext();
An understanding in Spring XML configuration is recommended before the Java based configuration.
- Java Configuration
- It uses Java classes annotated with
@Configuration
to define beans and their configurations, replacing XML configuration files.
- It uses Java classes annotated with
- ClassPath
- The configuration class must be in a package included in the classpath. A typical project structure places configuration classes in a package like
com.example.config
.
- The configuration class must be in a package included in the classpath. A typical project structure places configuration classes in a package like
- Implementation of ApplicationContext
- The
AnnotationConfigApplicationContext
class is used to load Java-based configuration, extendingAbstractApplicationContext
and implementingConfigurableApplicationContext
.
- The
- Dependency Injection
- The container resolves dependencies and injects them into beans based on
@Bean
methods or annotations like@Autowired
, supporting constructor, setter, or field injection.
- The container resolves dependencies and injects them into beans based on
Advantages of Java-based configuration
- Type-safety
- Java-based configuration is type-safe, meaning that the compiler checks for errors during compile time, such as invalid bean definitions or method calls.
- Readability
- Java-based configuration avoids the verbose syntax of XML configuration. The configuration logic is more readable and concise, written directly in Java classes.
- Flexibility and logic in configuration
- Java-based configuration allows for programmatic control, such as adding conditional logic, loops, or dynamic bean creation based on runtime parameters.
Create Java configuration
You need to create a Java class file in a new package under the classpath. In this example we name the configuration package com.example.config
and the Java configuration file inside the package AppConfig.java
.
- Create configuration class
Java-based configuration uses classes annotated with
@Configuration
. This annotation indicates that its primary purpose is as a source of bean definitions.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig { }
- Define bean
In the configuration class, beans are defined using the
@Bean
annotation.
@Configuration
public class AppConfig {
@Bean
public Person person() {
return new Person();
}
}
The AppConfig
class is equivalent to the following Spring <beans/>
XML:
<beans>
<bean id="person" class="com.example.Person"/>
</beans>
- Initialise the ApplicationContext
Use the
AnnotationConfigApplicationContext
class to load your configuration and retrieve beans.
import com.example.config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Person person = context.getBean("person", Person.class);
person.code();
}
}
public class Person {
public void code() {
System.out.println("Coding...");
}
}
Coding...
Customise bean name
By default, the bean name (bean ID) is the same as the method name. This functionality can be overridden, however, with the name
attribute, as the following example shows:
@Configuration
public class AppConfig {
@Bean(name = "myThing")
public Thing thing() {
return new Thing();
}
}
You can just pass the name itself.
@Configuration
public class AppConfig {
@Bean("myThing")
public Thing thing() {
return new Thing();
}
}
A bean can have multiple names using name array.
@Configuration
public class AppConfig {
@Bean(name = {"myThing1", "myThing2", "myThing3"})
public Thing thing() {
return new Thing();
}
}
Bean scope
Spring includes the @Scope
annotation so that you can specify the scope of a bean.
The default scope is singleton
, you can override this with the @Scope
annotation and set the ‘value’ attribute to your desired scope.
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Person person() {
// ...
}
}
Back to parent page: Spring
Web_and_App_Development Programming_Languages Java Spring_Framework Java_Based_Configuration Spring_Bean Bean_Scope
Reference: