Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java based configuration and scan

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?

like image 346
Stan Kurilin Avatar asked Oct 10 '12 11:10

Stan Kurilin


2 Answers

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 ...
     }
 }
like image 196
Nandkumar Tekale Avatar answered Sep 23 '22 18:09

Nandkumar Tekale


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.

like image 22
octo Avatar answered Sep 23 '22 18:09

octo