Junior — Middle
What methods can be used to implement object creation in Spring or similar frameworks?
sobes.tech AI
Answer from AI
In Spring and similar frameworks, object creation is usually implemented through inversion of control (IoC) and dependency injection (DI). The main methods are:
- Using annotations: for example,
@Component,@Service,@Repositoryfor automatic scanning and bean creation. - Using configuration classes: with
@Configurationand methods annotated with@Beanthat return the required objects. - Using XML configuration (deprecated method, but still supported).
Example of creating a bean via configuration:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
Spring will automatically create and manage the lifecycle of such objects.