自己定义一个注解,然后逻辑代码需要通过获取自定义注解的一个属性来进行权限控制。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
public String value();
}
接口与实现类
public interface UserService {
public void add();
public void del(int id);
public String queryName(String name);
}
@Service("userService")
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("执行UserServiceImpl中的add方法.....");
}
public void del(int id) {
System.out.println("执行UserServiceImpl中的del方法.....");
}
public String queryName(String name) {
System.out.println("执行UserServiceImpl中的queryName方法.....");
return null;
}
}
方式一:在接口方法上添加注解
public interface UserService {
@LogAnnotation("执行添加")
public void add();
@LogAnnotation("执行删除")
public void del(int id);
@LogAnnotation("执行根据名字查询")
public String queryName(String name);
}
定义一个切面类
@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAspect {
@Pointcut("execution(* com.spring.service..*.*(..))")
public void logPoint(){//声明切入点
}
//环绕通知
@Around("logPoint()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws NoSuchMethodException {
try {
//获得目标方法的签名对象
Signature signature = proceedingJoinPoint.getSignature();
System.out.println(signature.getDeclaringType());
//将目标方法的签名对象转化为MethodSignature
MethodSignature methodSignature= (MethodSignature) signature;
//获得方法的注解
Method method = methodSignature.getMethod();
//System.out.println(method);
Object target = proceedingJoinPoint.getTarget();
LogAnnotation declaredAnnotation =
method.getDeclaredAnnotation(LogAnnotation.class);
String value = declaredAnnotation.value();
System.out.println(value);
proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
方式二:在实现类的方法上添加注解
@Service("userService")
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("执行UserServiceImpl中的add方法.....");
}
public void del(int id) {
System.out.println("执行UserServiceImpl中的del方法.....");
}
public String queryName(String name) {
System.out.println("执行UserServiceImpl中的queryName方法.....");
return null;
}
}
切面代码:
@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAspect {
@Pointcut("execution(* com.spring.service..*.*(..))")
public void logPoint(){//声明切入点
}
//环绕通知
@Around("logPoint()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws NoSuchMethodException {
try {
//获得目标方法的签名对象
Signature signature = proceedingJoinPoint.getSignature();
//将目标方法的签名对象转化为MethodSignature
MethodSignature methodSignature= (MethodSignature) signature;
//获得方法的注解
Method method = methodSignature.getMethod();
Object target = proceedingJoinPoint.getTarget();
Method realMethod = (Method) target .getClass().getDeclaredMethod(signature.getName(),method.getParameterTypes());
LogAnnotation annotation = realMethod.getAnnotation(LogAnnotation.class);
System.out.println(annotation.value());
proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
测试代码:
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.add();
userService.del(10);
userService.queryName("11");
}
运行结果: