Java 如何仅在 Spring 中通过注解配置组件扫描?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24883987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:20:58  来源:igfitidea点击:

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

javaspringspring-annotations

提问by membersound

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

我正在 tomcat 网络服务器环境中运行一个War文件。

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

我有一个基于注释的配置@Beans,以及一个用于 webservices 的 xml 配置:

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

applicationContext.xml:

应用上下文.xml:

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

Problem: I would like to define @ComponentScanby 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.

问题:我想@ComponentScan通过注释定义仅具有类型安全性。但是,如果我这样做,则不会执行扫描。相比之下,当我使用时<context:component-scan..一切正常。

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

SpringWeb 服务器中的组件扫描是否与使用 xml 进行包扫描的配置相关联?

采纳答案by Swaraj

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

通过http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html类似的东西。

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