码迷,mamicode.com
首页 > 其他好文 > 详细

AOP

时间:2018-09-08 14:11:10      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:package   config   xsd   style   ann   ret   返回   ext   第一个   

在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点"

例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..))

execution()是最常用的切点函数,其语法如下所示:

 整个表达式可以分为五个部分:

 1、execution(): 表达式主体。

 2、第一个*号:表示返回类型,*号表示所有的类型。

 3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。

 4、第二个*号:表示类名,*号表示所有的类。

 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

定义AOP:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAop 
{
    @Pointcut("execution(* *.test(..))")
    public void test()
    {
        
    }
    
    @Before("test()")
    public void beforeTest()
    {
        System.out.println("this is before test");
    }
    
    @After("test()")
    public void afterTest()
    {
        System.out.println("this is after test");
    }
    
    @Around("test()")
    public Object arount(ProceedingJoinPoint p)
    {
        System.out.println("before.....1");
        Object o = null;
        try
        {
            o = p.proceed();
        }
        catch(Throwable e)
        {
            
        }
        System.out.println("after...1");
        return o;
    }

}
定义调用Aop的Bean
@Component("aopBean")
public class AopBean 
{
    public void test()
    {
        System.out.println("this is my test message.....");
    }

}


spring2.xml
<?
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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <aop:aspectj-autoproxy /> <context:annotation-config /> <context:component-scan base-package="com.donghua.aop"/> </beans>

 

测试:

ApplicationContext bf = new ClassPathXmlApplicationContext("spring2.xml");
AopBean aop = (AopBean) bf.getBean("aopBean");
aop.test();

输出:

before.....1
this is before test
this is my test message.....
after...1
this is after test

应用AOP后返回的是代理对象

AOP

标签:package   config   xsd   style   ann   ret   返回   ext   第一个   

原文地址:https://www.cnblogs.com/daxiong225/p/9608900.html

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