2017-04-07 67 views
0

我試圖創建一個只對包含用戶角色的單個請求有效的bean。在調用任何控制器方法之前,我將在@Around方法中填充角色。然後我需要稍後訪問這些角色以進行其他授權檢查。在@Around內彈簧請求作用域bean未能被自動裝入

@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':自動裝配依賴注入失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:private 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)}

Aspect在啓動時創建。我會雖然注入的請求範圍的bean將保持null,直到請求開始進入,然後我可以填充currentRoles豆的具體請求。

回答

0

很可能您忘記了您的Service類中的@Service註釋;)