2015-02-09 107 views
0

我正在使用以下代碼在Spring MVC應用程序中註冊Mbean。春季session範圍Mbean

@ManagedResource(objectName = "spring:name=TestService", description = "Test Mbean") 

@ManagedAttribute

這適用於單身和基於原型的範圍類型。對於基於會話和請求的範圍類型,我收到以下錯誤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mbeanExporter' defined in class org.springframework.context.annotation.MBeanExportConfiguration: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.calculatorService': 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. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410) ~[spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE] 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE] 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE] 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937) [catalina.jar:7.0.47] 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) [catalina.jar:7.0.47] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.47] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [catalina.jar:7.0.47] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [catalina.jar:7.0.47] 
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.7.0_75] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.7.0_75] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.7.0_75] 
    at java.lang.Thread.run(Unknown Source) [na:1.7.0_75] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.calculatorService': 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. 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:344) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.jmx.export.MBeanExporter.autodetect(MBeanExporter.java:880) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.jmx.export.MBeanExporter.autodetectBeans(MBeanExporter.java:842) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:521) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    ... 21 common frames omitted 
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) ~[spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE] 
    at org.springframework.web.context.request.SessionScope.get(SessionScope.java:90) ~[spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:330) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    ... 28 common frames omitted 

是否可以創建會話/請求範圍Mbeans? 如果是這樣,有沒有人有任何建議?

謝謝。

編輯:

@Scope(value="session") --> produces the error 

@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS`) --> Works! 

@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS) 
@ManagedResource(objectName = "spring:name=testService", description = "test Mbean") --> produces the same error again. 

回答

0

其實除了已經告訴你的proplem解決

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.calculatorService': 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;

的一點是,你嘗試引用會話/請求從一個作用域bean單例範圍的bean。在應用程序啓動期間,應用程序中沒有任何會話或請求處於活動狀態因此scoped bean沒有實例化。

通過定義代理可以解決此問題。

其實我覺得這個問題是spring scoped proxy bean

+0

嗨的副本,感謝您的回覆。我編輯了這個問題。我使用不帶@ManagedResource的代理。如果我添加它,同樣的錯誤。 – user1449511 2015-02-09 09:16:48

+0

爲什麼要將受管資源注入到會話作用域bean中?你爲什麼不抽象這個服務層? – questionare 2015-02-09 09:30:06