Skip to content

Instantly share code, notes, and snippets.

@wendal
Created April 8, 2012 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendal/2337195 to your computer and use it in GitHub Desktop.
Save wendal/2337195 to your computer and use it in GitHub Desktop.
NutAOP简单示例
package aop;
import org.nutz.aop.ClassAgent;
import org.nutz.aop.ClassDefiner;
import org.nutz.aop.DefaultClassDefiner;
import org.nutz.aop.InterceptorChain;
import org.nutz.aop.MethodInterceptor;
import org.nutz.aop.asm.AsmClassAgent;
import org.nutz.aop.matcher.MethodMatcherFactory;
public class UserAction {
public boolean login(String username, String password) throws Throwable {
if ("wendal".equals(username) && "qazwsxedc".equals(password)) {
System.out.println("登陆成功");
return true;
}
System.out.println("登陆失败");
return false;
}
private static ClassDefiner cd = new DefaultClassDefiner(UserAction.class.getClassLoader());
public static void main(String[] args) throws Throwable {
//无AOP的时候
UserAction ua = new UserAction(); //直接new,将按原本的流程执行
ua.login("wendal", "qazwsxedc");
System.out.println("-----------------------------------------------------");
System.out.println("-----------------------------------------------------");
//有AOP的时候
ClassAgent agent = new AsmClassAgent();
LogInterceptor log = new LogInterceptor();
agent.addInterceptor(MethodMatcherFactory.matcher("^login$"), log);
//返回被AOP改造的Class实例
Class<? extends UserAction> userAction2 = agent.define(cd, UserAction.class);
UserAction action = userAction2.newInstance();
action.login("wendal", "qazwsxedc");//通过日志,可以看到方法执行前后有额外的日志
}
}
class LogInterceptor implements MethodInterceptor {
public void filter(InterceptorChain chain) throws Throwable {
System.out.println("方法即将执行 -->" + chain.getCallingMethod());
chain.doChain();// 继续执行其他拦截器
System.out.println("方法执行完毕 -->" + chain.getCallingMethod());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment