等待花开的季节

<< JDK动态代理(Spring AOP理解的基础) | 首 页 | Spring的切入点Pointcut和Advisor学习(2) >> Spring的切入点Pointcut和Advisor学习 2008-02-27 Tag: Spring AOP Pointcut Advisor 版权声明 :转载时请以超链接形式标明文章原始出处和作者信息及 本声明 http://cmjerry.blogbus.com/logs/16068420.html 1.我们写好真正实现功能的业务代码 package cmj.advice; public interface ILogin { public boolean login(String username,String password); } package cmj.advice; public class LoginAction implements ILogin { public boolean login(String username, String password) { if("zhangsan".equals(username)) throw new RuntimeException(); if(username.equals("lisi")&&password.equals("1234")) return true; else return false; } } 2.写好业务代码以后,我们想加入其他的功能,比如权限管理和日志管理等。 现在就先编写好一个切面。 package cmj.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LoginAroundAdvice implements MethodInterceptor { private Log log=LogFactory.getLog(LoginAroundAdvice.class); public Object invoke(MethodInvocation invocation) throws Throwable { if(ILogin.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())) { String username=(String) invocation.getArguments()[0]; log.info("user"+"["+username+"]"+" is logining"); boolean bFlag=(Boolean)invocation.proceed(); if(bFlag) { log.info("user"+"["+username+"] login success"); } else { log.info("user"+"["+username+"] login failed"); } return bFlag; } return invocation.proceed(); } } 3.如果我们编写了切面,现在直接在spring里配置,那么这个切面是对所有的类的方法都起作用的。 4.现在我们想把切面使用到某些类的某些方法上,这就需要先定义切入点,所谓的切入点,也就是说 你的业务功能代码执行时加入切面。现在要做的就是定义切入点(在什么地方需要加入),一个业务功能代码要执行,当然要有个类,有个方法才行。那么我们的切入点就需要两个条件:1)哪些类需要加入切入点。2)加入切入的点的类的哪些方法加入切面? 那么我们就先定义好一个类过滤的东西和方法过滤掉东西。 package cmj.pointcut; import org.springframework.aop.ClassFilter; import cmj.advice.ILogin; public class MyClassFilter implements ClassFilter { public boolean matches(Class clazz) { if(ILogin.class.isAssignableFrom(clazz)) return true; else return false; } } package cmj.pointcut; import java.lang.reflect.Method; import org.springframework.aop.MethodMatcher; import cmj.advice.ILogin; public class MyMethodMatcher implements MethodMatcher { public boolean isRuntime() { return false; } public boolean matches(Method method, Class clazz) { String methodName=method.getName(); if("login".equals(methodName)&&ILogin.class.isAssignableFrom(clazz)) return true; else return false; } public boolean matches(Method arg0, Class arg1, Object[] arg2) { // TODO Auto-generated method stub return false; } } 现在就可以定义我们的切入点了 package cmj.pointcut; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; public class MyPointcut implements Pointcut { public ClassFilter getClassFilter() { return new MyClassFilter(); } public MethodMatcher getMethodMatcher() { // TODO Auto-generated method stub return new MyMethodMatcher(); } } 5.有了切入点,我们还需要知道在切入点的地方加入的切面是哪些切面? 我们可以把切入点和需要加入的切面放到advisor里。 例如: package cmj.pointcut; import org.aopalliance.aop.Advice; import org.springframework.aop.Pointcut; import org.springframework.aop.PointcutAdvisor; import cmj.advice.LoginAroundAdvice; public class MyAdvisor implements PointcutAdvisor { public Pointcut getPointcut() { return new MyPointcut(); } public Advice getAdvice() { // TODO Auto-generated method stub return new LoginAroundAdvice(); } public boolean isPerInstance() { // TODO Auto-generated method stub return false; } } 6.现在我们可以配置spring的bean了 ?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" http://www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd "> <!-- 真正实现业务逻辑的类 --> <bean id="loginTarget" class="cmj.advice.LoginAction"></bean> <!--配置拦截器--> <bean id="loginAroundAdvice" class="cmj.advice.LoginAroundAdvice"></bean> <!-- 配置代理 这样产生的代理对象就知道这个代理对象在执行哪些方法的时候加入什么样的切面--> <bean id="login" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 需要代理的目标对象 --> <property name="target"> <ref local="loginTarget"/> </property> <!-- 需要代理的目标对象实现的接口 --> <property name="proxyInterfaces"> <value>cmj.advice.ILogin</value> </property> <!-- 也就说实现真正业务逻辑代码真正执行前/后需要执行的代码--> <property name="interceptorNames"> <list> <idref local="myAdvisor"/> </list> </property> </bean> <bean id="myAdvisor" class="cmj.pointcut.MyAdvisor"></bean> 7.测试: package cmj.advice; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class TestAop extends TestCase { public void testAop() { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); ILogin login=(ILogin) context.getBean("login"); login.login("lisi", "1234"); } } 随机文章: Spring的IntroductionInterceptor 2008-02-27 Spring Remoting HttpInvoker产生异常的解决办法 2008-02-29 Spring Remoting(1) 2008-02-29 Spring的IntroductionInterceptor(2) 2008-02-28 JDK动态代理(Spring AOP理解的基础) 2008-02-26 收藏到: Del.icio.us 引用地址: cmjerry 发表于 19:15:04 | 编辑 | 继续话题 | 转发 | 推荐 发表评论

原始快照:web.archive.org