创建名称为“ scopedTarget.userPlatform”的bean时出错:作用域“用户”在当前线程中未激活

时间:2018-10-26 16:42:37

标签: spring-boot cumulocity

org.springframework.beans.factory.BeanCreationException:创建名称为'scopedTarget.userPlatform'的bean时出错:作用域'user'对于当前线程无效;如果您打算从单例中引用它,请考虑为此bean定义作用域代理。嵌套的异常是java.lang.IllegalStateException:不在任何上下文中!     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:355)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)     在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:705)     在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)     在com.cumulocity.sdk.client.PlatformImpl $$ EnhancerBySpringCGLIB $ 998b9bde.close()     在com.test.CleanAssignedSoftwareService.CreateSoftwareAssignmentOperations(CleanAssignedSoftwareService.java:235)     在com.test.ScheduledTask.reportCurrentTime(ScheduledTask.java:33)     在sun.reflect.GeneratedMethodAccessor69.invoke(未知来源)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)     在org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)     在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)     在java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)     在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 301(ScheduledThreadPoolExecutor.java:180)     在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748) 原因:java.lang.IllegalStateException:不在任何上下文中!     在com.cumulocity.microservice.context.ContextServiceImpl.getContext(ContextServiceImpl.java:41)     在com.cumulocity.microservice.context.annotation.EnableContextSupportConfiguration $ 2.getContextId(EnableContextSupportConfiguration.java:45)     在com.cumulocity.microservice.context.scope.BaseScope.doGetSynchronized(BaseScope.java:47)     在com.cumulocity.microservice.context.scope.BaseScope.get(BaseScope.java:39)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)     ...省略了19个常见框架

1 个答案:

答案 0 :(得分:0)

使用@Scheduled Annotation运行某些代码时,需要定义上下文。否则会出现此异常,因为使用累积性API的线程不在任何上下文中。

例如对于所有订阅的租户:

private final MicroserviceSubscriptionsService subscriptions;

@Scheduled(fixedDelay = 5 * 60 * 1000)
private void checkForAgentRepresentation() {
    for (final MicroserviceCredentials mc : subscriptions.getAll()) {
        final String tenant = mc.getTenant();
        subscriptions.runForTenant(tenant, () -> {
            //doSomething like
            inventoryApi.get(...);
        });
    }
}