首页 > 其他 > 详细

Spring注解实现AOP

时间:2014-02-23 00:07:48      阅读:564      评论:0      收藏:0      [点我收藏+]
 

------------------------------------------

 

要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:

<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-2.5.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

</beans>

 

 

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

基于XML配置方式声明切面。

基于注解方式声明切面

 

自定的切面:(注意我们的切面一定要交给Spring管理才行,也即在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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
 
@Aspect
public class MyIntercept {
 
    @Pointcut("execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))")
    private void anyMethod() {} // 定义一个切入点
     
     
    @Before("anyMethod() && args(name)"//利用arg来限定拥有一个参数, 参数的名字是String类型,然后得到该参数
    public void doAccessCheck(String name) {
        System.out.println("Name = " + name);
        System.out.println("前置通知");
    }
     
    @AfterReturning(pointcut = "anyMethod()", returning="name") //获得返回结果
    public void doAfterReturning(String name) {
        System.out.println("返回Name = " + name);
        System.out.println("后置通知");
    }
     
    @After("anyMethod()")
    public void doAfter() {
        System.out.println("最终通知");
    }
     
    @AfterThrowing(pointcut = "anyMethod()", throwing="e")
    public void doAfterThrowing(Exception e) {
        System.out.println(e.getMessage());
        System.out.println("例外通知");
    }
     
    @Around("anyMethod()")//环绕通知  可以在环绕通知里面坐权限的限定 struts 的连接器就是  定义格式是固定的
    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
<?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="myIntercept" class="cn.gbx.example.MyIntercept"></bean>
</beans>

  

Spring注解实现AOP

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

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