Junior — Middle
What methods can you use to implement component configuration description in Spring Framework?
sobes.tech AI
Answer from AI
In the Spring Framework, component configuration can be implemented in several ways:
- XML configuration — the traditional method where components and their dependencies are described in XML files.
<bean id="myService" class="com.example.MyService" />
- Java configuration (annotations @Configuration and @Bean) — configuration through Java classes, allowing the use of type safety and refactoring benefits.
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
- Automatic component scanning (@Component, @Service, @Repository, @Controller) — annotating classes for automatic detection and registration in the Spring context.
@Service
public class MyService {
// ...
}
- Using profiles (@Profile) — for defining configurations activated in different environments.
These methods can be combined depending on project requirements.