码迷,mamicode.com
首页 > 编程语言 > 详细

Spring之AOP

时间:2016-10-13 23:43:20      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

package org.zln.module.test3_aop.interceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
 * Created by coolkid on 2015/6/6 0006.
 */
/*声明这是一个切面*/
@Aspect
public class MyInterceptor {
    /*声明切入点 定义要拦截的方法 方法体是空的,使用表达式说明哪些方法是切入点*/
    @Pointcut("execution(* org.zln.module.test3_aop.service.impl.PersonServiceBean.*(..))")
    private void anyMethod(){}
    /*声明一个前置通知*/
    @Before("anyMethod()")
    public void doAccessCheck() {
        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);
    }
    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        //if(){//判断用户是否在权限
        System.out.println("进入方法");
        /*执行业务Bean方法*/
        Object result = pjp.proceed();
        System.out.println("退出方法");
        //}
        return result;
    }
}
/*
execution(* org.zln.module.test3_aop.service..*.*(..)) 说明
第一个*    返回值类型,*表示任何返回值类型
org.zln.module.test3_aop.service    包名
..  对子包下的类也要进行拦截
第二个*   类,*表示所有类
第三个*   方法,*表示所有方法
(..)    方法的参数,..表示任意
定义anyMethod方法,作为这个切入点的名称
execution(* org.zln.module.test3_aop.service.impl.PersonServiceBean.*(..)) 说明
对PersonServiceBean类的所有方法进行拦截
 */
<?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: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">
    <!--打开AOP注解解析器-->
    <aop:aspectj-autoproxy/>
    <!--将自定义拦截器交给Spring管理-->
    <bean id="myInterceptor" class="org.zln.module.test3_aop.interceptor.MyInterceptor"/>
    <bean id="personServiceAop" class="org.zln.module.test3_aop.service.impl.PersonServiceBean"/>
</beans>

 

Spring之AOP

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/5958484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!