现在的位置: 首页 > 综合 > 正文

spring采用注解方式实现AOP

2014年02月09日 ⁄ 综合 ⁄ 共 3036字 ⁄ 字号 评论关闭

package com.saly.service;

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;

/**
 * 必须交给spring管理
 * 注解方式:@Component
 * 配置方式:<bean id="myInterceptor" class="com.saly.service.MyInterceptor">
 * @author saly
 *
 */

@Aspect
public class MyInterceptor {

    /*
     * @Pointcut("execution (* com.saly.service..*.*.(..))")
     * 第一个※代表返回类型(任何类型),
     * 第一个..对子包进行拦截,
     * 第二个※对哪个类拦截(对所有类拦截),
     * 第三个※代表所有方法,括号中的两个..代表参数任意
     *
     */

    @Pointcut("execution (* com.saly.service.impl.PersonServiceBean.*(..))")//对所有方法进行拦截,参数任意
    private void anyMethod(){}//声明一个切入点
    
    @Before("anyMethod() && args(name)")//切入点的名称
    public void doAccessCheck(String name){//只能拦截到参数为一个切是string类型的方法
        System.out.println("前置通知"+name);
    }
    @AfterReturning(pointcut="anyMethod()",returning="result")//只能拦截返回值是string类型的方法
    public void doAccessCheckAfter(String result){
        System.out.println("后置通知"+result);
    }
    @After("anyMethod()")
    public void doAccessCheckFinally(){
        System.out.println("最终通知");
    }
    @AfterThrowing(pointcut="anyMethod()",throwing="e")//列外信息作为参数传入到doAfterThrowing方法中
    public void doAfterThrowing(Exception e){
        System.out.println("例外通知"+e);
    }
    //环绕通知,struts2做权限控制//可以替换以上其他方法
    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        //if(){//判断用户是否在权限
        System.out.println("进入方法");
        Object result = pjp.proceed();
        System.out.println("退出方法");
        //}
        return result;
    }
}

辅助类

public interface PersonService {
    public String getPersonName(Integer id);
    public void save(String name);
    public void update(String name,Integer id);
}

public class PersonServiceBean implements PersonService {
    public String getPersonName(Integer id) {
        System.out.println("我是getPersonName()方法");
        return "salyfan";
    }
    public void save(String name) {
        throw new RuntimeException("例外异常");
        //System.out.println("我是save()方法");
    }
    public void update(String name, Integer id) {
        System.out.println("我是update()方法");
    }

}

<?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:context="http://www.springframework.org/schema/context"
       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/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="myInterceptor" class="com.saly.service.MyInterceptor"/>
    <bean id="personService" class="com.saly.service.impl.PersonServiceBean"/>

</beans>

抱歉!评论已关闭.