回顾SpringAOP,简单的测试,加深理解
2016-02-25 15:10
633 查看
首先导入Spring的包,我是直接用MyEclipse导入了,我用的是Spring3.0
然后创建applicationContext.xml,进行配置
这里的正则筛选类是为了更好的模拟现实情况,也是提高自己的使用能力
下图是包和Java文件
先创建一个接口ComputerShop,内容如下:
再创建一个类ComputerA来实现这个接口,内容如下:
然后创建切面--我们需要做额外业务的类,内容如下:
最后创建一个测试类进行测试,内容如下:
结构如下图:
从结果可以看到,通过AOP可以在目标类的前后增加需要的情况来实现,本例子中的正则通过.*sell.*使所有卖的方法才会走beforeAdvice,进行了过滤选择,使得不同情况下的代码更完美!!
然后创建applicationContext.xml,进行配置
<!-- 代理的目标类 --> <bean id="c_A" class="com.tc.service.ComputerA"></bean> <!-- 切面 也就是额外的业务 --> <bean id="beforeadvice" class="com.tc.proxy.ProxyBeforeAdvice"></bean> <bean id="afteradvice" class="com.tc.proxy.ProxyAfterAdvice"></bean> <bean id="arounadvice" class="com.tc.proxy.ProxyAroundAdvice"></bean> <!-- 代理关系 --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 目标类 --> <property name="target"> <ref bean="c_A"> </ref> </property> <property name="interceptorNames"> <list> <value>afteradvice</value> <value>regex</value> <value>arounadvice</value> </list> </property> </bean> <!-- 正则筛选类 --> <bean id="regex" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="pattern"> <value>.*sell.*</value> </property> <property name="advice"> <ref bean="beforeadvice"> </ref></property> </bean>
这里的正则筛选类是为了更好的模拟现实情况,也是提高自己的使用能力
下图是包和Java文件
先创建一个接口ComputerShop,内容如下:
package com.tc.impl; /** * 商店接口 * @author tc * @date 24 Jan 2016 @time 14:03:18 */ public interface ComputerShop { //卖电脑 public void sellComputer(); //修电脑 public void repairComputer(); //卖手机 public void sellPhone(); }
再创建一个类ComputerA来实现这个接口,内容如下:
package com.tc.service; import com.tc.impl.ComputerShop; public class ComputerA implements ComputerShop{ @Override public void sellComputer() { System.out.println("卖电脑"); } @Override public void repairComputer() { System.out.println("维修"); } @Override public void sellPhone() { System.out.println("卖手机"); } }
然后创建切面--我们需要做额外业务的类,内容如下:
package com.tc.proxy; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class ProxyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("送鼠标"); } }
package com.tc.proxy; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class ProxyAroundAdvice implements MethodInterceptor{ @Override public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("促销"); Object o = arg0.proceed(); System.out.println("送货"); return o; } }
package com.tc.proxy; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class ProxyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("发传单"); } }
最后创建一个测试类进行测试,内容如下:
package com.tc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tc.impl.ComputerShop; public class Demo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ComputerShop cs = (ComputerShop) context.getBean("proxy"); cs.sellComputer(); System.out.println("-------------------"); cs.repairComputer(); } }
结构如下图:
从结果可以看到,通过AOP可以在目标类的前后增加需要的情况来实现,本例子中的正则通过.*sell.*使所有卖的方法才会走beforeAdvice,进行了过滤选择,使得不同情况下的代码更完美!!
相关文章推荐
- Spring Boot的日志管理
- JavaWeb学习总结(一)——JavaWeb开发入门
- Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案
- Java POI 导出EXCEL经典实现
- Java学习笔记
- Java常量池理解与总结
- 一次hibernate+c3p0+mysql连接池java.net.SocketException: Connection reset故障的解决笔记
- Java 使用stringTemplate导出大批量数据excel(百万级)
- IntelliJ IDEA 12 创建Web项目 教程 超详细版
- Java加载Class文件的原理机制
- Java基础09 类数据与类方法
- eclipse自动补全的设置
- Running R from Eclipse – StatET Features
- java的private修饰的方法没有多态
- Running R from Eclipse – StatET installation and Usage
- ActiveMQ与Spring整合
- java笔记--什么是接口回调,怎么用
- java logger学习
- 【解决方法】关于eclipse中logcat只显示level无其他内容
- java中,为什么弃用stop和suspend方法