Java-based configuration is the modern and recommended way to configure Spring applications.
Instead of using XML files, we use pure Java classes and annotations to define beans, enable component scanning, and configure the Spring container.
Java-based configuration means:
Spring beans and dependencies are configured using Java classes annotated with
@Configurationand@Bean, instead of XML.
Spring introduced this style in Spring 3+ to provide:
- type safety
- IDE refactoring support
- less boilerplate
- better readability
Java-based configuration mainly uses:
| Annotation | Purpose |
|---|---|
@Configuration |
Marks class as Spring configuration class |
@Bean |
Declares a bean |
@ComponentScan |
Enables component scanning |
@Import |
Imports another configuration class |
@PropertySource |
Loads properties from file |
- tells Spring that the class contains bean definitions
- acts as replacement for XML
<beans> - Spring enhances it using CGLIB proxy to maintain singleton semantics
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
}This class now becomes a Spring configuration class.
- equivalent to XML
<bean>tag - tells Spring to create, manage, and inject this object
@Configuration
public class AppConfig {
@Bean
public Engine engine() {
return new Engine();
}
}Spring will register engine() method return object as a Spring bean.
@Configuration
public class AppConfig {
@Bean
public Engine engine() {
return new Engine();
}
@Bean
public Car car() {
return new Car(engine());
}
}Because of @Configuration proxying, engine() is called once, preserving singleton semantics.
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);This replaces:
<beans>...</beans>Instead of declaring each bean manually, Spring can scan packages automatically.
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}Now Spring detects:
@Component
@Service
@Repository
@Controllerautomatically.
This is the most common real-world setup.
@Configuration
@ComponentScan("com.example")
public class AppConfig {
@Bean
public Engine engine() {
return new Engine();
}
}- manually declared beans using
@Bean - auto-detected beans using
@ComponentScan
@Component
class Car {
private final Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}No XML, no explicit bean wiring needed.
Spring resolves dependency using type-matching.
Large applications split configuration across files.
@Configuration
public class DBConfig { }@Configuration
public class ServiceConfig { }@Configuration
@Import({DBConfig.class, ServiceConfig.class})
public class AppConfig { }This avoids huge monolithic configuration classes.
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
}@Value("${app.name}")
private String name;| Feature | Java Config | XML Config |
|---|---|---|
| Type safety | High | None |
| Refactoring | Easy | Hard |
| Readability | High | Verbose |
| Boilerplate | Low | High |
| IDE support | Excellent | Limited |
| Modern usage | Recommended | Legacy |
Spring Boot internally uses Java config everywhere.
Typical Boot app:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}@SpringBootApplication includes:
@Configuration@EnableAutoConfiguration@ComponentScan
So Spring Boot is built fully on Java-based configuration.
| Aspect | @Configuration | @Component |
|---|---|---|
| Purpose | Define bean methods | Register class as bean |
| Proxy created | Yes | No |
| Bean method interception | Yes | No |
| Use case | Central configuration | Business components |
- Forgetting
@Configuration - Using
newinstead of letting Spring create bean - Mixing XML and Java without understanding precedence
- Overusing
@Beaninstead of component scanning - Creating multiple instances accidentally when removing
@Configuration
Configuration of Spring beans using Java classes instead of XML.
- type safety
- readability
- refactor support
- less boilerplate
@Configuration is specialized @Component that supports bean method proxying.
To declare and register a method result as a Spring bean.
@ComponentScan.
-
Java-based configuration is modern Spring standard
-
@Configuration+@Beanreplace XML -
Supports component scanning and dependency injection
-
Used internally by Spring Boot
-
Offers refactoring, IDE help, and strong typing
-
Cleaner and more maintainable approach