深入理解Sping——AOP的试水
2017-01-29 00:00
447 查看
深入理解Sping——AOP的试水
定义接口
public interface HelloWorld { void printHelloWorld(); void doPrint(); }
定义接口的实现
public class HelloWorldImpl1 implements HelloWorld { public void printHelloWorld() { System.out.println("Enter HelloWorldImpl1.printHelloWorld()"); } public void doPrint() { System.out.println("Enter HelloWorldImpl1.doPrint()"); return; } } public class HelloWorldImpl2 implements HelloWorld { public void printHelloWorld() { System.out.println("Enter HelloWorldImpl2.printHelloWorld()"); } public void doPrint() { System.out.println("Enter HelloWorldImpl2.doPrint()"); return; } }
定义Handler
public class LogHandler { public void LogBefore() { System.out.println("Log before method"); } public void LogAfter() { System.out.println("Log after method"); } } public class TimeHandler { public void printTime() { System.out.println("CurrentTime = " + System.currentTimeMillis()); } }
定义XML配置
//spring-context-aop.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="helloWorldImpl1" class="com.melon.spring.aop.helloworld.HelloWorldImpl1"/> <bean id="helloWorldImpl2" class="com.melon.spring.aop.helloworld.HelloWorldImpl2"/> <bean id="timeHandler" class="com.melon.spring.aop.helloworld.TimeHandler"/> <bean id="logHandler" class="com.melon.spring.aop.helloworld.LogHandler"/> <!--proxy-target-class 为true,基于类代理;为false,基于接口代理--> <aop:config> <!-- (1)aspect里面有一个order属性,order属性的数字就是横切关注点的顺序 (2)把logHandler定义在timeHandler前面,Spring默认以aspect的定义顺序作为织入顺序 --> <aop:aspect id="time" ref="timeHandler"> <aop:pointcut id="addAllMethod" expression="execution(* com.melon.spring.aop.helloworld.HelloWorld.*(..))"/> <aop:before method="printTime" pointcut-ref="addAllMethod"/> <aop:after method="printTime" pointcut-ref="addAllMethod"/> </aop:aspect> <aop:aspect id="log" ref="logHandler" order="2"> <aop:pointcut id="printLog" expression="execution(* com.melon.spring.aop.helloworld.HelloWorld.*(..))"/> <aop:before method="LogBefore" pointcut-ref="printLog"/> <aop:after method="LogAfter" pointcut-ref="printLog"/> </aop:aspect> </aop:config> </beans>
定义测试类
@Log4j2 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/spring-context-test.xml"}) @WebAppConfiguration public class AopTest { @Test public void testHelloWorld() { HelloWorld hw1 = (HelloWorld) SpringContextHolder.getBean("helloWorldImpl1"); HelloWorld hw2 = (HelloWorld) SpringContextHolder.getBean("helloWorldImpl2"); hw1.printHelloWorld(); System.out.println(); hw1.doPrint(); System.out.println(); hw2.printHelloWorld(); System.out.println(); hw2.doPrint(); } }
执行结果:
Log before method CurrentTime = 1485690199071 Enter HelloWorldImpl1.printHelloWorld() CurrentTime = 1485690199079 Log after method Log before method CurrentTime = 1485690199079 Enter HelloWorldImpl1.doPrint() CurrentTime = 1485690199079 Log after method Log before method CurrentTime = 1485690199079 Enter HelloWorldImpl2.printHelloWorld() CurrentTime = 1485690199085 Log after method Log before method CurrentTime = 1485690199085 Enter HelloWorldImpl2.doPrint() CurrentTime = 1485690199085 Log after method
相关文章推荐
- 深入理解Android之AOP
- 深入理解Spring AOP之二代理对象生成
- 深入理解Android之AOP
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解Android之AOP
- 深入理解AOP(二) 之APT开发
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解Spring AOP之基本概念
- 深入理解js--面向切面编程 AOP
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解Spring 之 源码剖析AOP(XML配置方式)
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- 深入理解Android之AOP
- 深入理解Spring的两大特征(IOC和AOP)
- 深入理解面向切面的编程AOP、AspectJ、Spring
- 深入理解Spring的两大特征(IOC和AOP)
- Spring中AOP深入理解
- 深入理解Spring的两大特征(IOC和AOP)