Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
The @Configuration
annotation in the Spring Framework is a class-level annotation which indicates that the class can be used by the Spring IoC container as a source of bean definitions. It's an alternative to XML-based configuration and is part of Spring's Java-based configuration approach.
When you use @Configuration
, you typically also use @Bean
annotations within the class to specify beans explicitly.
Example:
Define a Simple Service Class:
package com.example.service; public class HelloWorldService { public String sayHello() { return "Hello from HelloWorldService!"; } }
Use @Configuration
and @Bean
:
Create a configuration class that uses the @Configuration
annotation. Within this class, use the @Bean
annotation to define beans.
package com.example.config; import com.example.service.HelloWorldService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public HelloWorldService helloWorldService() { return new HelloWorldService(); } }
In the above class, the helloWorldService
method is annotated with @Bean
, which means it will return a bean that can be managed by the Spring IoC container. The bean's name will, by default, be the method name, which in this case is "helloWorldService".
Bootstrap the Application:
For a standalone application, you can use AnnotationConfigApplicationContext
to bootstrap the Spring container using your Java configuration:
package com.example; import com.example.config.AppConfig; import com.example.service.HelloWorldService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); HelloWorldService service = context.getBean(HelloWorldService.class); System.out.println(service.sayHello()); } }
When you run the Application
class, it will print the message from the HelloWorldService
.
Key Points:
@Configuration
classes are a direct replacement for XML-based configuration. They use @Bean
methods to define the beans and their dependencies.
@Configuration
classes can be composed together. One @Configuration
class can import another using the @Import
annotation.
@Configuration
classes can also use @ComponentScan
to specify packages to scan for components, reducing the need to define each bean explicitly with a @Bean
method.
When working with Spring Boot, @Configuration
classes play a crucial role, especially when you want to define custom beans or override the auto-configuration provided by Spring Boot.
Using @Configuration annotation in Spring:
@Configuration
annotation indicates that a class declares one or more @Bean
methods and may be processed by the Spring container to generate bean definitions and service requests for those beans.// AppConfig.java import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { // Configuration details... }
Spring @Configuration example project:
@Configuration
annotation.// MyBean.java public class MyBean { // Bean logic... } // AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
Creating Java-based configuration with @Configuration in Spring:
@Configuration
to create Java-based configuration classes.// MyConfig.java import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { // Configuration details... }
Defining beans with @Configuration in Spring framework:
@Bean
methods within a class annotated with @Configuration
.// AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
Combining XML and Java-based configuration with @Configuration:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- XML configuration --> </beans>
// AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
Conditional bean definition with @Configuration in Spring:
@Configuration
.// AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Conditional; @Configuration public class AppConfig { @Bean @Conditional(MyCondition.class) public MyBean myBean() { return new MyBean(); } }
Importing other configurations using @Configuration in Spring:
@Import
.// AnotherConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AnotherConfig { @Bean public AnotherBean anotherBean() { return new AnotherBean(); } }
// AppConfig.java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(AnotherConfig.class) public class AppConfig { // Configuration details... }