gpt4 book ai didi

java - "Error creating bean with name ' scopedTarget.get '"接收 RabbitMQ 消息时

转载 作者:行者123 更新时间:2023-12-02 11:28:30 25 4
gpt4 key购买 nike

当我没有请求范围时,我在忽略@Scope时遇到一些问题。我可以解释一下。

我正在使用 Hibernate Envers 在审计表中保存用户的一些信息。当存在请求(来 self 的 Rest Controller )时,这很有效,因为需要记录用户,因此在这种情况下总是会发现用户。

但是,我也在使用 RabbitMQ。当我收到一些需要对数据库执行某些操作的消息(例如插入一个实体)时,我的代码尝试从应用程序上下文加载 User bean。 User Bean 与 @Scope 请求绑定(bind)在一起,Spring 找不到该请求的线程绑定(bind)请求。

所以,我收到错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.get': 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:355)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:705)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.domain.config.UserConfiguration$User$$EnhancerBySpringCGLIB$$c7e6e64e.getUserId(<generated>)
at com.domain.audit.RevisionEntityListener.newRevision(RevisionEntityListener.java:28)
at org.hibernate.envers.internal.revisioninfo.DefaultRevisionInfoGenerator.generate(DefaultRevisionInfoGenerator.java:93)
at org.hibernate.envers.internal.synchronization.AuditProcess.getCurrentRevisionData(AuditProcess.java:114)
at org.hibernate.envers.internal.synchronization.AuditProcess.executeInSession(AuditProcess.java:96)
at org.hibernate.envers.internal.synchronization.AuditProcess.doBeforeTransactionCompletion(AuditProcess.java:153)
at org.hibernate.envers.internal.synchronization.AuditProcessManager$1.doBeforeTransactionCompletion(AuditProcessManager.java:46)
at org.hibernate.engine.spi.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:928)
... 38 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)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
... 50 common frames omitted

我想忽略这种情况,因为在这种情况下我的意图是用“系统”之类的内容填充“用户”信息。表明此更改不是由用户直接进行的(因为没有用户参与)。

我的代码如下。

RevisionEntityListener(Hibernate Envers):

@Component
public class RevisionEntityListener implements RevisionListener, ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override
public void newRevision(Object revisionEntity) {

RevEntity revEntity = (RevEntity) revisionEntity;

User user = applicationContext.getBean(User .class);
revEntity.setUserId(user.getUserId());
// code
}

private static void setStaticApplicationContext(ApplicationContext context) {
applicationContext = context;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
setStaticApplicationContext(applicationContext);
}

}

我的用户配置:

@Configuration
public class UserConfiguration {

@Autowired
private OAuth2ClientContext context;

@Bean
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, scopeName = "request")
public User get() {
User user = null;
try {

String token = context.getAccessToken().getValue();
// code
return user;
}

@Getter
@Setter
@ToString
public static class User {
private Long userId;
// all user information
}

}

最佳答案

我不知道这是否是最好的选择,但我创建了一个 if看看是否 SecurityContextHolder.getContext().getAuthentication()存在:

User user;
if (SecurityContextHolder.getContext().getAuthentication() != null) {
user = applicationContext.getBean(User .class);
} else {
user = createSystemUser();
}
revEntity.setUserId(user.getUserId());

关于java - "Error creating bean with name ' scopedTarget.get '"接收 RabbitMQ 消息时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49457233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com