使用注解 实现 切面编程
1.定义注解
@Target({ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface MsgSecCheck {
String value();
int idx() default 0;
}
2.定义切面
@Aspect
@Component
public class MsgSecCheckAspect {
@Autowired
WeixinMiniService weixinMiniService;
/**
* 切入点
*/
@Pointcut("@annotation(com.sandun.app.config.msgsec.MsgSecCheck)")
public void methodArgs() {
}
/**
* 操作
*
* @param joinPoint
*/
@Before("methodArgs()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
MsgSecCheck msgSecCheck = method.getAnnotation(MsgSecCheck.class);
try {
Object arg = joinPoint.getArgs()[msgSecCheck.idx()];