2013-06-25 106 views
1

我是Spring的新手,試圖讓事情滾動。我想使用基於xml setter的依賴注入,並避免Java中的'@whatever'標記。Spring 3.1依賴注入失敗

我在Spring 3.1.1中使用了NetBeans 7,並且在項目中也有一些Struts 2代碼,並且工作正常。

我有的問題是,依賴注入似乎並沒有工作(accountService保持NULL),所以我猜我沒有正確配置。任何幫助,將不勝感激!

BaseActionUserAware是需要注射類:

public class BaseActionUserAware extends BaseAction 
{ 
    protected AccountService accountService; 
    protected String username; 

    @Override 
    public String execute() { 
     super.execute(); 

     // accountService = new AccountServiceImpl(); 

     Object accountID = session.get(SessionProperties.ACCOUNT_ID); 
     if(null != accountID) { 
      AccountBean account = accountService.getAccount((int)accountID); 
      username = account.getUsername(); 
     } 

     return SUCCESS; 
    } 

    public void setAccountService(AccountService accountService) { 
     this.accountService = accountService; 
    } 

    public String getUsername() { 
     return username; 
    } 
} 

...註釋代碼表示想什麼我來完成;我希望accountService被注入AccountServiceImpl。

這裏是我的WEB-INF/applicationContext.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <bean id="accountService" class="hyperbook.service.impl.AccountServiceImpl"/> 

    <bean id="baseActionUserAware" class="hyperbook.action.BaseActionUserAware"> 
     <property name="accountService" ref="accountService"/> 
    </bean> 

</beans> 

...我仔細檢查過的完全限定類名,這些都是正確的。

我最近增加了以下我的WEB-INF/web.xml文件,但它並沒有在所有幫助:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 

同樣,任何幫助將真正理解。謝謝!!

[編輯]我還應該提到 - BaseActionUserAware永遠不會在Java中的任何地方特別實例化。相反,它專門用於Struts 2操作類的擴展。因此,applicationContext.xml中的baseActionUserAware定義可能完全錯誤/不需要。我對Spring還不夠了解。

+0

近距離投票甚至更加怪異。 –

回答

1

您需要使用Struts 2 Spring插件,否則Spring不知道它應該對S2操作做任何事情。一旦你將S2插件放入(使用Maven以使其他依賴項自動出現),你基本上是在配置好Spring上下文加載器監聽器後完成的。

你不需要定義在Spring配置文件你的行動,但你可能。無論您是否需要取決於您的接線方式,例如,如果您按名稱自動接線,則可以跳過它。

+0

非常感謝你戴夫!我下載並將.jar添加到項目中,並立即生效!對於任何後來遇到此問題並且還需要該jar的人,請在此處獲取它:http://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin – user2507377

+0

@ user2507377我建議*強烈*反對下載隨機庫:請使用依賴管理工具。 –