在Spring中会话作用域Mbean

时间:2015-02-09 07:37:37

标签: java spring session spring-mvc web

我正在使用以下代码在Spring MVC应用程序中注册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.

1 个答案:

答案 0 :(得分:0)

实际上,异常已经告诉你解决问题的方法

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。在应用程序启动期间,您的应用程序中没有任何会话或请求处于活动状态。因此,范围内的bean没有实例化。

通过定义代理,您可以解决此问题。

实际上我认为此问题与spring scoped proxy bean

重复