我很新的春天是好的;)引導@Configuration
我有一個共享的模塊,我會打電話給「核心」和一對夫婦的項目,這取決於核心。這些子項目有spring/app-context.xml
文件,並且在它們被執行的環境中,spring被另一個框架初始化。
在「核心」的項目,我想使用定義一些單身人士服務的@Configuration方法:然後
@Configuration
public class AppConfig
{
@Bean
public MyService myService()
{
return new MyServiceImpl();
}
}
核心封裝在一個罐子,包括在分項目的類路徑。
如何引導核心模塊的服務,使它們可用於子項目中的@Autowire?
編輯:
我遇到的錯誤是:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [package.MyService] 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)}
我已經試過以下每個(完全)的:
<context:component-scan base-package="package" />
<import resource="classpath*:spring/core-context.xml" /><!-- defined in core.jar with MyService as a bean -->
<bean class="package.AppConfig" />
但只添加這子項目的app-context.xml似乎解決了錯誤:
<bean id="MyService" class="package.MyServiceImpl" />
...或者我並不完全依賴於使用@Configuration方法,但是我必須能夠在覈心模塊中配置beans/services,而不必在子項目中重新配置它們 – pstanton 2013-04-08 03:02:29
您是否採取了看看log4j日誌?在DEBUG級別日誌記錄中,您應該能夠看到更多關於爲什麼找到「AppConfig」而不是「MyService」的信息。 – 2013-04-08 04:08:27
嗨尼古拉斯,謝謝你的提示。打開彈簧日誌,我可以看到平臺在我的AppConfig上拋出了一個ClassNotFound,這意味着在初始化Spring應用程序上下文之前,容器尚未加載模塊的完整類路徑。我將不得不接受平臺的供應商。 – pstanton 2013-04-08 09:51:49