![](/img/trans.png)
[英]Spring application context has my bean but @autowired doesn't see it
[英]Spring, Multi module application context, B depends on A but cannot see bean
初始化bean之前,以下應用程序上下文是否應跨不同的* -context.xml加載所有bean定義? 我發現我的一個類無法找到依賴的bean,除非我將其標記為lazy-init,這樣bean才有機會加載。 但是我認為在初始化任何bean之前先讀取整個上下文。
另外,這可能與多個配置位置有關,而不是使用<import.../>
標簽<import.../>
bean
我的結構如下:
APP.ear
|-- utilJars
| |-- A.jar
| |-- A-API.jar
| |-- B.jar
| |-- B-API.jar
| `-- CONTROL.jar
`-- WEB.war
。
<!-- in CONTROL.jar -->
<bean id="ear.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>classpath*:META-INF/**/*-context.xml</value>
</list>
</constructor-arg>
</bean>
parentContextKey
與價值classpath:META-INF/beanRefContext.xml
啟動應用程序時,在初始化期間收到以下消息:
org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath:META-INF/beanRefContext.xml], factory key [ear.context];
`-> nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ear.context' defined in class path resource [META-INF/beanRefContext.xml]: Instantiation of bean failed;
`-> nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception;
`-> nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myService' is defined.
有人可以幫我理解Spring應用程序上下文的配置和實例化如何工作,以便我可以解決此謀殺案嗎?
更新: 文檔指出:
在構造基於XML的應用程序上下文時,位置字符串可以使用特殊的classpath *:前綴:
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");
這個特殊的前綴指定必須獲取與給定名稱匹配的所有類路徑資源(內部,這實際上是通過ClassLoader.getResources(...)調用發生的),然后合並以形成最終的應用程序上下文定義。
感謝您的時間。
A.jar沒有A-API.jar的清單條目,因此看不到該接口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.