2013-02-02 156 views
0

我是Spring AOP的新手,努力實現以下目標。Spring AOP參數化註釋

我想定義一個註解@RequirePermission它接受一個參數字符串或枚舉,並基於該字符串/枚舉我會做一些建議計算。因此,應該首先驗證在具有@RequiredPermissions的Controller中定義的任何方法。這是我的代碼到目前爲止。

看點:

package com.myapp.aspect; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 
import com.myapp.security.RequirePermissionType; 
import com.myapp.security.RequirePermission; 
@Aspect 
public class RequirePermissionAspect { 

    @Pointcut("within(@org.springframework.stereotype.Controller *)") 
    public void controllerBean() {} 

    @Pointcut("execution(@com.myapp.security.RequirePermission * com.myapp.controller.*.*(..))") 
    public void methodPointcut() {} 

    @Before("controllerBean() && methodPointcut() ") 
    public void afterMethodInControllerClass(com.myapp.security.RequirePermissionType name) { 
     System.out.println("before advice.."); 
     System.out.println("before advice.."+name.name()); 
    } 

譯註:

package com.myapp.security; 

public enum RequirePermissionType { 
    VIEW, MANAGE, IMPORT, SUPER; 
} 

@Documented 
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Inherited 
public @interface RequirePermission { 
    /** 
    * 
    * @return 
    */ 
    RequirePermissionType name() default RequirePermissionType.VIEW ; 
} 

現在,如果使用控制器方法註釋如下面的代碼工作:

@RequestMapping(value={"create"}) 
    @RequirePermission 
    public String createRegion(Model model){ 
    } 

但下面不工作:

@RequestMapping(value={"create"}) 
    @RequirePermission(name=RequirePermissionType.VIEW) 
    public String createRegion(Model model){ 
    } 

任何人都可以指導我缺少什麼,以及如何實現這一目標。

+0

無論如何,得到它的工作使用這種 '@Pointcut(「執行(@ com.gdev.vim.security.RequirePermission * com.gdev.vim.controller * *(..。 ))&& @annotation(name)「)' – Saqib

+0

:然後請分享你答案 –

+0

更改後的代碼如下。 (@com.myapp.security.RequirePermission * com.myapp.controller。*。*(..))&& @annotation(name)「) public void methodPointcut(com.myapp.security。 RequirePermission name){} @Before(「controllerBean()&& methodPointcut(name)」) public void afterMethodInControllerClass(com.myapp.security.RequirePermissionType name){ System.out.println(「before advice ..」) ; System.out.println(「before advice ..」+ name.name()); }' – Saqib

回答

1

@vamslip這裏是更新的方面類。休息都保持不變。 更新看點:

package com.myapp.aspect; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 
import com.myapp.security.RequirePermissionType; 
import com.myapp.security.RequirePermission; 
@Aspect 
public class RequirePermissionAspect { 

@Pointcut("within(@org.springframework.stereotype.Controller *)") 
public void controllerBean() {} 

@Pointcut("execution(@com.myapp.security.RequirePermission * com.myapp.controller.*.*(..)) && @annotation(name)") 
public void methodPointcut(com.myapp.security.RequirePermission name) {} 

@Before("controllerBean() && methodPointcut(name) ") 
public void afterMethodInControllerClass(com.myapp.security.RequirePermissionType name) { 
    System.out.println("before advice.."); 
    System.out.println("before advice.."+name.name()); 
} 
}