Junior — Middle
What methods can be used to configure and set up components in the Spring framework?
sobes.tech AI
Answer from AI
In Spring, component setup and configuration can be done in several ways:
-
XML configuration — the traditional method, where components are described in XML files using
<bean>tags. -
Annotations — a more modern and convenient approach. For example,
@Component,@Service,@Repositoryfor defining beans,@Autowiredfor dependency injection. -
Java configuration — using classes annotated with
@Configurationand methods with@Beanthat return component objects.
Example of Java configuration:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
These methods can be combined, choosing the style that best fits the project.