2011-09-29 33 views
7

我使用JSF 2作爲視圖,Spring使用業務邏輯。我想設置一個會話範圍使用註釋我的春天豆一(@Scope(「會議」)),但我得到這個異常:在Spring Beans中使用會話作用域

SEVERE: Context initialization failed 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handleFiles': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private creazione.util.FTPOperations creazione.components.HandleOldFiles.operations; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ftpOperations': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 
nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? 
If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 

我知道RequestContextListener。它在我的web.xml中。我還添加了RequestContextFilter兩個:

<listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
    <listener> 
     <listener-class> 
      org.springframework.web.context.request.RequestContextListener 
     </listener-class> 
    </listener> 

    <filter> 
     <filter-name>requestContextFilter</filter-name> 
     <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>requestContextFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>INCLUDE</dispatcher> 
     <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> 

似乎沒有任何工作。我究竟做錯了什麼? 謝謝!

回答

10

嘗試定義必須注入與aop:scoped-proxy會話的bean。

<bean id="ftpOperations" class="..." scope="session"> 
    <aop:scoped-proxy/> 
</bean> 

添加也是相關的命名空間,如果它不存在:如果您使用的是基於註解配置

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:aop="http://www.springframework.org/schema/aop" 
    ... 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     ... 
+3

是啊,但我使用的註解。我有相當多的spring bean在applicationContext.xml中編寫。是否有與等效的註釋? – spauny

+0

嘗試看看這個問題http://stackoverflow.com/questions/4503606/annotation-equivalent-of-aopscoped-proxy – stivlo

+0

仍然沒有工作......我已經添加@Scope(value =「session」 ,proxyMode = ScopedProxyMode.INTERFACES),我爲每個spring bean重構了一個接口,但是現在AutoWiring不再識別我的bean了...... – spauny

26

,只是在你的主配置XML改變這種標籤:

<context:component-scan base-package="my.package"/> 

<context:component-scan base-package="my.package" scoped-proxy="targetClass" /> 

也可以通過註釋標記使用代理的類:

@Component 
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 

這個工作適合我。 更多細節在這裏:4.12 Classpath scanning, managed components and writing configurations using Java

此外,如果你是通過創建上下文XML配置Bean,這裏是這種情況下另一個例子:

<bean id="somebean" class="com.package.beans" scope="session"> 
    <aop:scoped-proxy/> 
</bean> 
+0

嗨,這沒有爲我工作。所有三種方式我得到相同的錯誤不能連線。 –

+0

爲什麼它不是autowired有很多規則 - 破壞的範圍設置,沒有cglib它的類路徑(如果你是沒有接口的自動裝配的話)。有關更多詳細信息,請參閱錯誤消息 – msangel

相關問題