Spring AOP

关于aop,个人的理解就是在执行某个方法之前、之后或执行时等几个期间执行另一个方法。例如,有一个方法sleep(),实现是睡觉,在睡觉前要先玩下手机,定义一个类SleepHelper并继承接口MethodBeforeAdvice,这个接口下的方法的实现就是在睡觉之前要做的事,而如何在执行睡觉这个动作之前执行玩手机这个动作,就要通过xml配置文件或注解来配置

 

1.通过xml文件配置
Java代码:
1 //接口类
2 public interface ISleepable {
3     void sleep();
4 }
接口类
1 //实现类
2 public class Human implements ISleepable{
3     public void sleep() {
4         System.out.println("我要睡觉了");
5     }
6 }
实现类
 1 //通知类
 2 public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{
 3     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
 4         System.out.println("我已经睡着了");
 5     }
 6  
 7     public void before(Method method, Object[] objects, Object o) throws Throwable {
 8         System.out.println("睡觉前要玩会手机");
 9     }
10 }
通知类
1 //测试代码
2 public class DoTest {
3     @Test
4     public void iWannaSleep() {
5         ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
6         ISleepable human = (ISleepable) context.getBean("humanProxy");
7         human.sleep();
8     }
9 }
测试代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5           http://www.springframework.org/schema/beans
 6           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " >
 7     <bean id="sleepHelper" class="sample2.SleepHelper"/>
 8     <bean id="human" class="sample2.Human"/>
 9     <!--正则表达式切点,配置所有sleep结尾的方法--> 
10     <bean id="sleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
11         <property name="pattern" value=".*sleep" />
12     </bean>
13     <!--通知--> 
14     <bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
15         <property name="advice" ref="sleepHelper"/>
16         <property name="pointcut" ref="sleepPointcut"/>
17     </bean>
18     <!--代理--> 
19     <bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
20         <property name="target" ref="human"/>
21         <property name="interceptorNames" value="sleepHelperAdvisor"/>
22         <property name="proxyInterfaces" value="sample2.ISleepable"/>
23     </bean>
24 </beans>
ApplicationContext.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5           http://www.springframework.org/schema/beans
 6           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " >
 7     <bean id="sleepHelper" class="sample2.SleepHelper"/>
 8     <bean id="sleepAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
 9         <property name="advice" ref="sleepHelper"/>
10         <property name="pattern" value=".*sleep"/>
11     </bean>
12     <bean id="humanProxy" class="sample2.Human"/>
13     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
14 </beans>
另一个ApplicationContext.xml配置文件

 

 

2.通过注解

除测试代码和配置外,其他一样

1 //测试代码
2 public class DoTest {
3     @Test
4     public void iWannaSleep() {
5         ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
6         ISleepable human = (ISleepable) context.getBean("human");
7         human.sleep();
8     }
9 }
测试代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5        http://www.springframework.org/schema/beans/spring-beans.xsd
 6        http://www.springframework.org/schema/aop
 7        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 8     <bean id="human" class="sample2.Human"/>
 9     <bean id="sleepHelper" class="sample2.SleepHelper"/>
10  
11     <!-- 自动扫描被aspectj注解的类 -->
12     <aop:aspectj-autoproxy />
13 </beans>
配置文件

 

 

另附我的pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6  
 7     <groupId>maven-springaop-demo</groupId>
 8     <artifactId>com.tom</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10  
11     <dependencies>
12         <dependency>
13             <groupId>junit</groupId>
14             <artifactId>junit</artifactId>
15             <version>4.9</version>
16         </dependency>
17         <dependency>
18             <groupId>org.springframework</groupId>
19             <artifactId>spring-context</artifactId>
20             <version>4.2.4.RELEASE</version>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework</groupId>
24             <artifactId>spring-core</artifactId>
25             <version>4.2.4.RELEASE</version>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework</groupId>
29             <artifactId>spring-beans</artifactId>
30             <version>4.2.4.RELEASE</version>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework</groupId>
34             <artifactId>spring-expression</artifactId>
35             <version>4.2.4.RELEASE</version>
36         </dependency>
37         <dependency>
38             <groupId>commons-logging</groupId>
39             <artifactId>commons-logging</artifactId>
40             <version>1.2</version>
41         </dependency>
42         <dependency>
43             <groupId>org.aspectj</groupId>
44             <artifactId>aspectjweaver</artifactId>
45             <version>1.8.1</version>
46         </dependency>
47         <dependency>
48             <groupId>aopalliance</groupId>
49             <artifactId>aopalliance</artifactId>
50             <version>1.0</version>
51         </dependency>
52         <dependency>
53             <groupId>asm</groupId>
54             <artifactId>asm</artifactId>
55             <version>3.3.1</version>
56         </dependency>
57         <dependency>
58             <groupId>cglib</groupId>
59             <artifactId>cglib</artifactId>
60             <version>2.2.2</version>
61         </dependency>
62  
63     </dependencies>
64  
65 </project>
pom.xml

 

posted @ 2016-02-17 14:28  Netop  阅读(184)  评论(0)    收藏  举报