Stan Kurilin
Stan Kurilin

Reputation: 15802

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?

Upvotes: 5

Views: 3594

Answers (2)

octo
octo

Reputation: 665

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.

Upvotes: 3

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

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 ...
     }
 }

Upvotes: 15

Related Questions