2012-01-10 97 views
2

想象一個dao類和一個服務(BO)類。如何自動注入FwObject到setFwObject方法中,由於具有與用戶有關的信息,並且這些代碼段FwObject應內部框架被寫入彈簧動態對象注入 - @Autowired

Class MyAction exdends ActionSupport{ 
    @Autowired 
    private SomeService someService; 

    @Autowired 
    FwBean fwObj; 

    execute(){ 
      fwObj.set(fromRequest()); //build this object using request and session values 
    }  
} 


@Service 
class PersonServiceImpl {   
    @Autowired 
    private SomeDAO someDao;    
} 



@Repository 
    class PersonDAO { 
     @Autowired 
     public void setFwBean(FwBean obj) { 
     System.out.println(obj); 
     this.fwObject = obj //object will be come from MyAction 
    } 
} 

  <filter> 
      <filter-name>struts2</filter-name> 
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
     </filter> 

     <filter-mapping> 
      <filter-name>struts2</filter-name> 
      <url-pattern>/*</url-pattern> 
     </filter-mapping> 

     <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath: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> 

    </web-app> 


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

    <!-- scans the classpath for annotated components (including @Repostory 
    and @Service that will be auto-registered as Spring beans -->  
    <bean id="fwBean" class="com.sriseshaa.fw.helper.FwBean" scope="request"> 

      <!-- this next element effects the proxying of the surrounding bean --> 
      <aop:scoped-proxy/> 
    </bean> 

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

    <!-- methods or classes needing to run in a complete transaction will 
    be annotated with Transactional --> 
    <tx:annotation-driven /> 

    <!-- Creates a data source that can provide a connection to in-memory embedded database populated 
    with test data 
    see: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ch12s08.html --> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost/fwtest" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 


    <!-- This will ensure that hibernate or jpa exceptions are automatically translated into 
     Spring's generic DataAccessException hierarchy for those classes annotated with Repository 
     For example see PersonDaoJpa--> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

    <!-- JPA Entity Manager Factory --> 
    <bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
      p:dataSource-ref="dataSource" 
      p:persistenceXmlLocation="META-INF/persistence.xml" 
      p:persistenceUnitName="springJpaPersistenceUnit" />   


    <!-- bean post-processor for JPA annotations --> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 


    <!-- Transaction Config --> 
    <bean id="transactionManager" 
      class="org.springframework.orm.jpa.JpaTransactionManager" 
      p:entityManagerFactory-ref="entityManagerFactory"/> 

    <!-- use declarative transaction management --> 
    <tx:annotation-driven transaction-manager="transactionManager"/> 




</beans> 

異常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fwBean': Scope 'request' 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. 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:619) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fwBean': Scope 'request' 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. 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:339) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:653) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:604) 
    at com.sriseshaa.fw.helper.FwBean$$EnhancerByCGLIB$$79187402.toString(<generated>) 
    at com.sriseshaa.fw.helper.dao.AbstractDAO.setFwObject(AbstractDAO.java:86) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:582) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) 
    ... 23 more 
Caused by: 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. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) 
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325) 
    ... 36 more 

如何設置FwBean的請求級範圍,因爲我們必須仔細地創建新的FwBea n每個請求的實例或每個請求都有自己的FwBean實例

+0

和問題是什麼? – soulcheck 2012-01-10 14:49:29

+0

我不完全確定問題是什麼。爲什麼不autowire'SomeDao'以及? – 2012-01-10 14:50:10

+0

如何注入FwObject到使用註釋的setFwObject方法 – Arun 2012-01-10 14:56:45

回答

5

對於注入工作,FwObject應該是一個spring bean。如果您想遵循工廠模式,Spring將使用beans中的factory-method屬性來支持它。

@Repository 
    class SomeDAO { 
    @Autowired 
    public void setFwObject(FwObject obj) { 
    this.fwObject = obj 
    } 
} 

應自動裝配FwObject,如果它是Spring bean的話。否則使用getAutowireCapableBeanFactory()並使用autowire方法。

對於請求不活動的錯誤, 你嘗試使用

<aop:scoped-proxy/> 

作爲參考here

If you want to inject a (for example) HTTP request scoped bean into another bean, you 
will need to inject an AOP proxy in place of the scoped bean. That is, you need to 
inject a proxy object that exposes the same public interface as the scoped object, but 
that is smart enough to be able to retrieve the real, target object from the relevant 
scope (for example a HTTP request) and delegate method calls onto the real object. 
0

問題定義爲setter方法內。我試圖調用fwbean.toString()方法。去除行之後(//的System.out.println(OBJ);)它工作正常

@Repository 
    class PersonDAO { 
     @Autowired 
     public void setFwBean(FwBean obj) { 
     // System.out.println(obj); defect line 
     this.fwObject = obj //object will be come from MyAction 
    } 
}