首页 > 其他 > 详细

Spring XML配置实现AOP

时间:2014-02-23 01:05:26      阅读:328      评论:0      收藏:0      [点我收藏+]

1:  首先我们要定义 配置成切面的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.gbx.example;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
 
 
public class MyXmlIntercept {
     
    public void doAccessCheck() {
     
        System.out.println("前置通知");
    }
     
     
    public void doAfterReturning() {
        System.out.println("后置通知");
    }
     
    public void doAfter() {
        System.out.println("最终通知");
    }
     
    public void doAfterThrowing() {
        System.out.println("例外通知");
    }
     
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("进入方法");
        Object value = pjp.proceed();
        System.out.println("退出方法");
        return value;
    }
}
 
    

  

然后再XML文件中将其配置成切面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         
         <aop:aspectj-autoproxy/>
         
         <bean id="personService" class="cn.gbx.serviceimpl.PersonServiceImpl"></bean>
         
         <bean id="myXmlIntercept" class="cn.gbx.example.MyXmlIntercept"></bean>
         <aop:config>
            <aop:aspect id="myasp" ref="myXmlIntercept">
                <aop:pointcut expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))" id="mycut"/>
                <aop:before method="doAccessCheck" pointcut-ref="mycut"/>
                <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
                <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
                <aop:after pointcut-ref="mycut" method="doAfter"/>
                <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
            </aop:aspect>
         </aop:config>
</beans>

  

然后测试即可。

 

 简单说明

1
expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))"<br>如果我们要要返回值烈性为Stirng的 方法才被拦截
1
execution (java.lang.String cn.gbx.serviceimpl.PersonServiceImpl.*(..))<br><br>如果我们要求参数第一个为String , 后边不限
1
expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(java.lang.String, ..))"<br><br>要求返回至为非void
1
expression="execution (!void cn.gbx.serviceimpl.PersonServiceImpl.*(..))"<br><br><br>
1
<br><br>

Spring XML配置实现AOP

原文:http://www.cnblogs.com/E-star/p/3560964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!