Search code examples
javaspringspring-annotations

How to configure component-scan by annotation only in Spring?


I'm running a war file on a tomcat webserver environment.

I have an annotation based config for @Beans, and a xml config for webservices:

@Configuration
//@ComponentScan(basePackageClasses = ...)
public class AppConfig {
    //beans @Bean
}

applicationContext.xml:

<beans>
    <context:component-scan base-package="..."/>
    <jaxws:endpoint ... />
</bean>

Problem: I would like to define @ComponentScan by annotation only to have typesafety. But if I do so, the scanning is not performed. In contrast, when I use <context:component-scan.. everything works fine.

Is Spring component scanning within a webserver tied to configuration with xml for the package scanning?


Solution

  • Go through http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html Something Like.

        @Configuration
        @ComponentScan("com.company") // search the com.company package for @Component classes
        @ImportXml("classpath:com/company/data-access-config.xml")        
        public class Config {
        }