In my xml configuration files I can write
<context:component-scan base-package="com.my.stuff"/>
Now I move on java based configuration. How I can do it in my @Configuration
based class without having ApplicationContext
there?
You can use @ComponentScan annotation to your configuration class.
Example :
@Configuration
@ComponentScan("com.acme.app.services")
public class AppConfig {
@Bean
public MyBean myBean() {
// instantiate, configure and return bean ...
}
}
In case of spring < 3.1 you can use http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.html in @PostConstruct method of your @Configuration class. Of course you need to autowire ApplicationContext into you @Configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With