2011-09-29 17 views
7

Sto utilizzando JSF 2 per la vista e Spring per la business logic. Sto cercando di impostare un ambito sessione per uno dei miei fagioli molla con annotazioni (@Scope ("sessione")), ma sto ottenendo questa eccezione:Utilizzo di Session Scope in 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. 

so di RequestContextListener. È nel mio web.xml. Ho anche aggiunto 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> 

Niente sembra funzionare. Che cosa sto facendo di sbagliato? Grazie!

risposta

10

Provare a definire i bean che devono essere iniettati come sessione con aop: scoped-proxy.

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

Add anche lo spazio dei nomi in questione, se non è già presente:

<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

Sì, ma io sto usando le annotazioni. Ho un sacco di fagioli primaverili da scrivere in applicationContext.xml. Esiste un'annotazione equivalente con ? – spauny

+0

prova a guardare questa domanda http://stackoverflow.com/questions/4503606/annotation-equivalent-of-aopscoped-proxy – stivlo

+0

Ancora non funziona ... Ho aggiunto @Scope (value = "session" , proxyMode = ScopedProxyMode.INTERFACES), ho refactored un'interfaccia per ogni bean spring, ma ora AutoWiring non riconosce più i miei bean ... – spauny

26

Se si sta utilizzando la configurazione di annotazione base, basta cambiare nella tua XML di configurazione principale di questo tag:

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

a

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

Inoltre è possibile marcare classe per lavorare con delega tramite annotazioni:

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

questo lavoro per me. Maggiori dettagli qui: 4.12 Classpath scanning, managed components and writing configurations using Java

Anche se siete creare fagioli tramite la configurazione contesto XML, Ecco un altro esempio di questo caso:

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

Ciao, questo non ha funzionato per me. Tutti e tre i modi ottengo lo stesso errore non potrebbe collegare. –

+0

Ci sono molte motivazioni per cui non è autorizzata - impostazioni dell'ambito non valide, in mancanza di classpath (nel caso in cui tu sia autowire senza interfacce). Vedi il tuo messaggio di errore per maggiori dettagli. – msangel

Problemi correlati