2013-04-04 85 views
1

,我發現了以下內容:Spring上下文掃描行爲

2013-03-27 18:51:54,944 ERROR pringframework.web.context.ContextLoader: 227 - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exporter' defined in URL ... Cannot resolve reference to bean 'dynamicNamingStrategy' while setting bean property 'namingStrategy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dynamicNamingStrategy' is defined 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) 
    at 

和STS也抱怨:

Multiple annotations found at this line: 
    - Referenced bean 'dynamicNamingStrategy' 
    not found 

與下面的Spring上下文設置:

在一個項目中,我具有以下(摘錄)的jmx管理上下文(core-app-web-common-management-context.xml):

<context:annotation-config /> 

<context:component-scan 
    base-package="com.a.b.c.management.*" /> 


<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" 
    lazy-init="false"> 
    <property name="autodetect" value="true"></property> 
    <property name="namingStrategy" ref="dynamicNamingStrategy"/> 

其中dynamicNamingStrategy在不同項目中定義(和打包成一個不同的罐),並在被稱爲上述組件的掃描,如下

package com.a.b.c.management; 


@Component("dynamicNamingStrategy") 
public class DynamicNamingStrategy extends KeyNamingStrategy { 
...... 

上面Spring上下文被依次導入到主背景設在同一個項目:

<import resource="classpath*:/META-INF/spring/core-app-web-common-management-context.xml"/> 

所以,不知何故@Component掃描DynamicNamingStrategy是不工作...

如果我插件TEAD使用這個屬性定義在出口國,那麼它的工作原理:

<property name="namingStrategy"><bean class="com.a.b.c.management.DynamicNamingStrategy" /></property> 

,但我想明白爲什麼組件掃描機制,在第一時間上不去,顯然我失去了一些東西。

此外,在STS春Explorer視圖中缺少的是豆腐爲好。啓用對配置文件中的元素選項的支持似乎沒有什麼區別。

回答

1

將您的包名稱末尾的.*放在您的<context:component-scan>元素base-package屬性中。我認爲這是導致組件掃描尋找名爲*的軟件包,其中可能沒有一個(不能是一個)。

基礎包就是這個基礎包。 component-scan將掃描所有基礎軟件包中的所有子項,因此不需要在軟件包名稱末尾添加.*通配符。