nutz实现aop
时间: 2023-07-08 17:24:05 浏览: 55
Nutz框架实现AOP主要有两种方式:
1. 基于注解的AOP:使用@Aop注解标注切面类,然后在需要切入的方法上使用@AopInvoke注解标注即可。
例如:
```java
@Aop(value = "log")
public class LogAop {
@AopInvoke
public void beforeInvoke(Method method, Object[] args) {
System.out.println("before invoke");
}
}
```
2. 基于接口的AOP:创建一个实现了MethodInterceptor接口的拦截器类,然后在需要切入的方法上使用@Interceptor注解标注即可。
例如:
```java
@IocBean
public class LogInterceptor implements MethodInterceptor {
public void filter(InterceptorChain chain) throws Throwable {
System.out.println("before invoke");
chain.doChain();
System.out.println("after invoke");
}
}
```
```java
@At("/hello")
@Ok("json")
@Interceptor("log")
public String hello(){
return "hello world";
}
```
这里使用了基于接口的AOP方式,使用了LogInterceptor拦截器,在hello方法上使用了@Interceptor("log")注解标注。