Reputation: 1472
Do we have a spring annotation
that provides an option to initialize a bean (not a component
) if not available through default constructor while autowiring
?
If yes, that will be awesome. I am tired of initializing beans in some configuration class using default constructor and it occupies space.
I am doing this currently
@Bean
public Test test() {
return new Test();
}
Expecting:
Sometime like:
@Autowire(initMethodType=constructor)
private Test test:
If no, was there no real need of such annotation or any technical limitation?
Upvotes: 0
Views: 1827
Reputation: 494
You could annotate your class with @Component
, like this:
@Component
public class Test {
...
}
Whereever you need that class, you could then simply autowire it:
@Autowired
private Test test;
You would just have to make sure, that you use @ComponentScan
to pick up that bean. You can find more information on that here.
Upvotes: 0
Reputation: 1617
You have to use @Bean
annotation inside an @Configuration
class.
Check the following link
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
Upvotes: 1