【问题标题】:How to configure component-scan by annotation only in Spring?如何仅在 Spring 中通过注释配置组件扫描?
【发布时间】:2014-09-13 01:45:42
【问题描述】:

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

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

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

applicationContext.xml:

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

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

网络服务器中的Spring 组件扫描是否与用于包扫描的 xml 配置相关联?

【问题讨论】:

标签: java spring spring-annotations


【解决方案1】:

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

【讨论】:

  • 如上所述,这正是我所拥有的,它不能与 tomcat 上的其他 applicationContext.xml 结合使用。
  • 我认为您需要导入 xml。我在@ComponentScan 之后添加了它
  • 现在是 @ImportResource 而不是 ImportXml
猜你喜欢
  • 2015-01-13
  • 2011-10-15
  • 1970-01-01
  • 2019-01-11
  • 2013-12-23
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多