扫描多模块maven spring项目服务模块中的组件

时间:2012-12-13 21:13:20

标签: spring maven

我有一个带有以下结构的Maven Multi Module项目:

  • 站点父
    - Site-UI
    - 现场实体
    - 现场服务
    - Sites-Repository


我的Site-UI web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.mysite.app.web.spring</param-value>
`</context-param>


com.mysite.app.web.spring中的AppConfig.java:

@Configuration
@ImportResource({ "classpath*:contextConfig.xml",
        "classpath*:securityConfig.xml" })
@ComponentScan(basePackages = "com.mysite.app.web.controller")
public class AppConfig
{
    @Bean
    public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter()
    {
        ...
    }

    @Bean
    public SpringTemplateEngine templateEngine()
    {
        ...
    }

    ...
}


com.mysite.app.web.spring中的WebConfig.java:

@EnableWebMvc
@ComponentScan({ "com.mysite.app.web.controller" })
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
{

    public WebConfig()
    {
        super();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry reg)
    {
        reg.addInterceptor(new BaseInterceptor());
        reg.addInterceptor(new SecurityInterceptor());
        //reg.addInterceptor(new AddCardInterceptor());
    }
}


这是我的一个控制器:

@Controller
public class PurchaseController extends AppController
{
    static Logger logger = LoggerFactory.getLogger(PurchaseController.class);

    @Autowired
    private IPurchaseService purchaseService;

        ...
}


启动应用程序时出现以下错误:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'purchaseController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mysite.services.IPurchaseService com.mysite.app.web.controller.PurchaseController.purchaseService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mysite.services.IPurchaseService com.mysite.app.web.controller.PurchaseController.purchaseService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 23 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 25 more

我知道这是因为我的服务没有被扫描过。

我的服务模块中有以下内容:

@Configuration
@ComponentScan(basePackages = "com.mysite.services")
public class ServicesConfig
{
}

我需要知道的是如何配置我的应用程序进行扫描? 我希望在代码中尽可能多地维护配置并避免使用xml文件。

由于

1 个答案:

答案 0 :(得分:0)

找出解决方案。

我需要为每个模块指定spring配置文件的包位置,我在Site-UI模块的web.xml中执行此操作。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.mysite.app.web.spring com.mysite.services.spring com.mysite.persistence.spring</param-value>
</context-param>

可能有更好的方法可以做到这一点,我愿意接受建议。