5

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?

2 Answers 2

15

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 ...
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.