【发布时间】:2017-04-07 20:00:19
【问题描述】:
我正在尝试创建一个仅对包含用户角色的单个请求有效的 bean。在调用任何控制器方法之前,我将在 @Around 方法中填充角色。然后我需要稍后访问这些角色以进行其他授权检查。
@Component
@Aspect
public class SecurityAudit {
@Autowired
private CurrentRoles currentRoles;
@Around("@annotation(requestMapping) && execution( *
com.myapp.controller..*.*(..))")
public Object around(ProceedingJoinPoint pjp, RequestMapping requestMapping)
throws Throwable {
...
...
//I populate the roles with a db lookup. They will be referenced here, and later in controller methods as-needed.
}
}
package com.myapp.model;
...
...
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"sso",
"roles"
})
@Component
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class CurrentRoles {
@JsonProperty("roles")
private Set<Role> roles;
...
...
}
我得到以下信息:
org.springframework.beans.factory.BeanCreationException:创建名为“securityAudit”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.myapp.model.CurrentRoles com.myapp.currentRoles;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [com.myapp.model.CurrentRoles] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
方面是在启动时创建的。虽然注入的请求范围的 bean 会在请求开始进入之前保持为空,但我会为该特定请求填充 currentRoles bean。
【问题讨论】:
标签: spring spring-boot scope autowired