gpt4 book ai didi

java - Aspect 中的 session 作用域 bean

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:33 25 4
gpt4 key购买 nike

我在将 session 范围 bean Autowiring 到方面时遇到问题。

我的外观是这样的:

@Aspect
public class AuditAspect {

Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private AuditService auditService;

@Autowired
private SessionData sessionData;


@AfterReturning(value = "@annotation(fasthiAudit) && execution(* *(..))")
public void audit(JoinPoint joinPoint, FasthiAudit fasthiAudit) {
final String className = joinPoint.getTarget().getClass().getName();
final String methodName = joinPoint.getSignature().getName();

try {

UserId userId = sessionData.getUserId();
TenantId tenantId = sessionData.getTenantId();

} catch (Exception e) {
logger.error("Could not log audit entry for method name: " + methodName + " in class " + className, e);
}

}

}

我的 SessionData bean 是 session 范围的,如下所示:

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionData {

private UserId userId;
private TenantId tenantId;

public UserId getUserId() {
return userId;
}

public void setUserId(UserId userId) {
this.userId = userId;
}

public TenantId getTenantId() {
return tenantId;
}

public void setTenantId(TenantId tenantId) {
this.tenantId = tenantId;
}
}

在这方面,AuditService 是 Autowiring 的,SessionData 不为 null,但它会抛出一个异常,例如

Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate se.reforce.fasthi.core.infrastructure.SessionData$$EnhancerBySpringCGLIB$$26c0d5bb.toString()

我添加了一个 ContextLoaderListener 来公开请求,如下所示:

event.getServletContext().addListener(new RequestContextListener());

在 SessionData bean 中 Autowiring 作为其他单例 bean 中的代理工作正常,但问题出现在方面

我错过了什么?

谢谢/约翰

最佳答案

我在头痛了几天后发现了这个问题。问题是我的 Vaadin 集成(我忘记在问题中提及)。我的 vaadin UI 上的 @Push 注释做了一些与 servlet 混淆的事情,因此 spring 无法识别 session 范围的 beans。我解决了这个问题,将注释更改为:

@Push(transport= Transport.WEBSOCKET_XHR)

就是这样,现在 session 作用域 bean 与 singleton bean 完美配合

关于java - Aspect 中的 session 作用域 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813835/

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