简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM