Spring configuration refers to the process of setting up beans and their dependencies in a Spring application context. It allows developers to define how objects are created, how they are wired together, and how they behave in the application.
@Configuration
annotations to define beans and dependencies.In XML-based configurations, the application context is defined in an XML file. Here’s a simple structure:
1. Create Bean Classes
// MyBean.java
package com.example;
public class MyBean {
private MyDependency dependency;
public void setDependency(MyDependency dependency) {
this.dependency = dependency;
}
public void display() {
System.out.println("Dependency: " + dependency.getMessage());
}
}
// MyDependency.java
package com.example;
public class MyDependency {
public String getMessage() {
return "Hello from MyDependency!";
}
}
2. Create XML Configuration File
3. Load Application Context and Use Beans
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
myBean.display(); // Output: Dependency: Hello from MyDependency!
}
}
Annotation-based configurations allows for a more concise way to define beans using annotations directly in the Java classes.
@Component
: Indicates a Spring-managed component.@Autowired
: Injects dependencies automatically.@Configuration
: Marks a class as a source of bean definitions.@Bean
: Indicates that a method produces a bean to be managed by the Spring container.1. Create Bean Classes with Annotations
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final MyDependency dependency;
@Autowired
public MyBean(MyDependency dependency) {
this.dependency = dependency;
}
public void display() {
System.out.println("Dependency: " + dependency.getMessage());
}
}
@Component
public class MyDependency {
public String getMessage() {
return "Hello from MyDependency!";
}
}
2. Create Java Configuration Class
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.display(); // Output: Dependency: Hello from MyDependency!
}
}
Java-based configuration is similar to annotation-based configuration but focuses more on programmatically defining beans in Java classes.
// MyBean.java
package com.example;
public class MyBean {
private MyDependency dependency;
public void setDependency(MyDependency dependency) {
this.dependency = dependency;
}
public void display() {
System.out.println("Dependency: " + dependency.getMessage());
}
}
// MyDependency.java
package com.example;
public class MyDependency {
public String getMessage() {
return "Hello from MyDependency!";
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyDependency myDependency() {
return new MyDependency();
}
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setDependency(myDependency());
return myBean;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.display(); // Output: Dependency: Hello from MyDependency!
}
}
Externalized configuration allows you to manage properties outside the code, making it easier to manage different environments (e.g., development, testing, production).
# application.properties
dependency.message=Hello from Externalized Configuration!
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyDependency {
@Value("${dependency.message}")
private String message;
public String getMessage() {
return message;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class AppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("application.properties"));
return configurer;
}
@Bean
public MyDependency myDependency() {
return new MyDependency();
}
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setDependency(myDependency());
return myBean;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.display(); // Output: Dependency: Hello from Externalized Configuration!
}
}