深入理解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