2010-04-14 35 views
2

我用@Resource註解bean類,@Autowired自動裝配依賴, 和Spring配置文件中的這些東西:無豆命名爲「...」的定義和Spring @Resource註釋

 
    context:component-scan base-package="package1,package2" 
    tx:annotation-driven 

所以,正常工作(測試)。 Spring使用@Resource註解 掃描package1,package2,類,然後使用getBean()如果從CONSOLE應用程序[例如main()函數]進行測試,我就可以得到它們。

但是當我嘗試用另一個方法(使用Spring容器管理的環境=使用Tomcat):

 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:beans.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

編譯罐子所有的bean類,並把這個罐子到WEB-INF/lib目錄

然後我看到了什麼?我無法getBean()任何這些@Resource註釋的豆!
春天根本找不到它們。
我仍然可以在beans.xml中顯式出現getBean()bean。


問題在哪裏?

+0

您能否重新格式化您的第一個代碼段? – lexicore 2010-04-14 17:06:14

回答

0

我不確定它是如何在獨立模式下工作的,但在Spring上下文中的「」元素允許使用「@Resource」註釋。查看Spring文檔以獲取更多信息。

+0

@David M. Karr你的意思是你不確定?我說,我測試了這個東西,在獨立模式下它工作正常,在web-app和Spring之間的凝聚力問題。 – EugeneP 2010-04-14 15:15:45

+1

我說我不確定它是如何獨立工作的,因爲我看到你的配置有問題,它不會在獨立應用程序和Web應用程序中解決。 我也看到我試圖提供的關鍵信息被省略,因爲我沒有逃過尖括號。你需要有「context:annotation-config」元素。 – 2010-04-15 15:19:17

2

不見了<context:annotation-config/>

<context:annotation-config/>  
<context:component-scan base-package="package1"/> 
<context:component-scan base-package="package2"/> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

or 

<context:annotation-config/>  
<context:component-scan base-package="package1, package2"/> 
<tx:annotation-driven transaction-manager="transactionManager" /> 
相關問題