[英]Spring AOP Parameterized Annotation
I am new to spring AOP and struggling to achieve the following. 我是春季AOP的新手,正在努力实现以下目标。
I want to define an annotation @RequirePermission which accepts a parameter String or enum and based on that string/enum I will do some calculations in advice. 我想定义一个注释@RequirePermission,它接受一个参数String或enum,并基于该字符串/枚举我将在建议中进行一些计算。 So, any method defined in Controller having @RequiredPermissions should be validated first.
因此,应该首先验证Controller中具有@RequiredPermissions的任何方法。 Here is my code so far.
到目前为止,这是我的代码。
Aspect: 方面:
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());
}
Annotation: 注解:
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 ;
}
Now if use the annotation in controller method as below the code works: 现在,如果在下面的控制器方法中使用注释,则代码可以正常工作:
@RequestMapping(value={"create"})
@RequirePermission
public String createRegion(Model model){
}
But the following does not work: 但是以下方法不起作用:
@RequestMapping(value={"create"})
@RequirePermission(name=RequirePermissionType.VIEW)
public String createRegion(Model model){
}
Any one could guide me what I am missing and how do I achieve this. 任何人都可以指导我我所缺少的以及如何实现这一目标。
@vamslip here is the updated aspect class. @vamslip这里是更新的方面类。 Rest all stays the same.
其余所有保持不变。 Updated Aspect:
更新的方面:
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.