spring-纯pojo切面

一.导包

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
二.代理类
 1 package com.yztc.pojo;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 
 5 /**
 6  * _ooOoo_
 7  * o8888888o
 8  * 88" . "88
 9  * (| -_- |)
10  * O\ = /O
11  * ___/`---'\____
12  * .   ' \\| |// `.
13  * / \\||| : |||// \
14  * / _||||| -:- |||||- \
15  * | | \\\ - /// | |
16  * | \_| ''\---/'' | |
17  * \ .-\__ `-` ___/-. /
18  * ___`. .' /--.--\ `. . __
19  * ."" '< `.___\_<|>_/___.' >'"".
20  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
21  * \ \ `-. \_ __\ /__ _/ .-` / /
22  * ======`-.____`-.___\_____/___.-`____.-'======
23  * `=---='
24  * .............................................
25  *
26  * @author bindu
27  * @date 2017-10-19 14:04
28  */
29 
30 
31 public class SleepAspect {
32 
33 
34 
35     public void before(){
36         System.out.println("睡觉之前拖衣服");
37     }
38     public void afterReturning(){
39         System.out.println("睡觉");
40     }
41     public void afterThrowing(Exception ex) throws Exception {
42         System.out.println("出大事了,有bug");
43         System.out.println(ex.getMessage());
44     }
45     public Object around(ProceedingJoinPoint pjp) throws Throwable{
46         Object proceed =null;
47         if (!"".equals("admin")){
48             System.out.println("核心方法被执行");
49             proceed = pjp.proceed(pjp.getArgs());
50             System.out.println("核心方法执行完");
51         }
52         return proceed;
53     }
54 }

三.代理接口和实现类

1 package com.yztc.pojo;
2 
3 public interface SleepDao {
4     public String sleep(String name) throws Exception;
5 }
 1 package com.yztc.pojo;
 2 
 3 /**
 4  * _ooOoo_
 5  * o8888888o
 6  * 88" . "88
 7  * (| -_- |)
 8  * O\ = /O
 9  * ___/`---'\____
10  * .   ' \\| |// `.
11  * / \\||| : |||// \
12  * / _||||| -:- |||||- \
13  * | | \\\ - /// | |
14  * | \_| ''\---/'' | |
15  * \ .-\__ `-` ___/-. /
16  * ___`. .' /--.--\ `. . __
17  * ."" '< `.___\_<|>_/___.' >'"".
18  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
19  * \ \ `-. \_ __\ /__ _/ .-` / /
20  * ======`-.____`-.___\_____/___.-`____.-'======
21  * `=---='
22  * .............................................
23  *
24  * @author bindu
25  * @date 2017-10-19 14:01
26  */
27 
28 
29 public class SleepDaoImpl implements SleepDao {
30     @Override
31     public String sleep(String name) throws Exception {
32         if (!name.equals("xm")){
33             throw new Exception("睡你妹啊,起来嗨杯");
34         }
35         System.out.println("开始睡觉");
36         return "小明";
37     }
38 }

纯pojo切面 ,配置xml文件:spring-context.xml

代码不变,只是修改配置文件,加入AOP配置即可

 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        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 6 
 7     <bean id="sleep" class="com.yztc.pojo.SleepDaoImpl"/>
 8     <bean id="sleepAspect" class="com.yztc.pojo.SleepAspect"/>
 9 
10     <!--proxy-target-class="true" 表示使用cglib代理-->
11     <!--配置AOP-->
12     <aop:config>
13         <!--切面-->
14         <aop:aspect id="aspect" ref="sleepAspect">
15             <!--切入点的配置-->
16             <aop:pointcut id="pointcut" expression="execution(* com.yztc.pojo.*.Sleep.*(..))"/>
17 
18             <aop:before method="before" pointcut-ref="pointcut"/>
19             <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"/>
20             <aop:around method="around" pointcut-ref="pointcut"/>
21             <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
22         </aop:aspect>
23     </aop:config>
24 </beans>

四.测试

 1 package com.yztc.pojo;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 /**
 6  * _ooOoo_
 7  * o8888888o
 8  * 88" . "88
 9  * (| -_- |)
10  * O\ = /O
11  * ___/`---'\____
12  * .   ' \\| |// `.
13  * / \\||| : |||// \
14  * / _||||| -:- |||||- \
15  * | | \\\ - /// | |
16  * | \_| ''\---/'' | |
17  * \ .-\__ `-` ___/-. /
18  * ___`. .' /--.--\ `. . __
19  * ."" '< `.___\_<|>_/___.' >'"".
20  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
21  * \ \ `-. \_ __\ /__ _/ .-` / /
22  * ======`-.____`-.___\_____/___.-`____.-'======
23  * `=---='
24  * .............................................
25  *
26  * @author bindu
27  * @date 2017-10-19 15:22
28  */
29 
30 
31 public class TestMain {
32 
33 
34 
35     public static void main(String[] args) {
36         String xm=null;
37         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
38         SleepDao sleep = (SleepDao) context.getBean("sleep");
39         try {
40             xm = sleep.sleep("xm");
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44         System.out.println(xm);
45     }
46 }

 

posted @ 2017-10-19 16:05  友情天  阅读(764)  评论(0编辑  收藏  举报