Spring AOP之AspectJ

来源:互联网 发布:电脑接口转换器 知乎 编辑:程序博客网 时间:2024/05/21 10:43

Spring中使用AspectJ来实现AOP有两种方式
一.通过注解

@Aspectpublic class MyAspectJ{    @Pointcut("execution(public * eat(..)) && target(com.chm.test.Person)")    public void pointcut()    {    }    @Before("pointcut()")    public void beforeMethod(JoinPoint joinPoint)    {        System.out.println("前置方法执行");    }    @Around("pointcut()")    public void aroundMehtod(ProceedingJoinPoint joinPoint)    {        System.out.println("before");        try        {            joinPoint.proceed();        }        catch (Throwable e)        {            e.printStackTrace();        }        System.out.println("after");    }    @After("pointcut()")    public void afterMethod(JoinPoint point)    {        System.out.println("后置方法执行");    }}
<bean id="person" class="com.chm.test.Person">        <property name="name" value="charming"></property>    </bean>    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> <!-- 关键点 -->    <bean id="myaspectj" class="com.chm.test.MyAspectJ"></bean>

这里我们了解一下AspectJ的表达式
execution(修饰符? 返回类型 声明类型? 方法名(参数类型) 异常类型?)
执行任意public方法
execution(public * * (..))
执行任意以set开头的方法
execution(* set*(..))
执行任何至少有一个参数,且第一个参数为String类型的方法
execution(* * (java.lang.String, ..))
执行Service类中的任何方法
execution(* com.chm.test.Service.*(..))
执行com.chm.test包及其子包中的任意方法
execution(* com.chm.test.*. *(..))

二. 在Spring 配置文件中配置

public class MyAspectJ{    public void beforeMethod(JoinPoint joinPoint)    {        System.out.println("前置方法执行");    }    public void aroundMehtod(ProceedingJoinPoint joinPoint)    {        System.out.println("before");        try        {            joinPoint.proceed();        }        catch (Throwable e)        {            e.printStackTrace();        }        System.out.println("after");    }    public void afterMethod(JoinPoint point)    {        System.out.println("后置方法执行");    }}
<?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:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" <!-- 增加命名空间 -->    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <bean id="person" class="com.chm.test.Person">        <property name="name" value="charming"></property>    </bean>    <bean id="myAspectJ" class="com.chm.test.MyAspectJ"></bean>    <aop:config>        <aop:aspect ref="myAspectJ">            <aop:pointcut expression="execution(public * *(..))" id="pointcut"/>            <aop:before method="beforeMethod" pointcut="execution(public * eat(..))"/>            <aop:around method="aroundMehtod" pointcut-ref="pointcut"/>            <aop:after method="afterMethod" pointcut-ref="pointcut"/>        </aop:aspect>    </aop:config>   </beans>
0 0
原创粉丝点击