亲宝软件园·资讯

展开

Spring AOP开发

ClayBin 人气:0

AspectJ简介

注解开发

环境准备

<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
        <!--需要单独引入-->
        <aop:aspectj-autoproxy/>
</beans>

不同的通知类型

最通知中通过value属性定义切点

--匹配所有类public方法 execution(publice * * (..))---第一个*:任意返回值 第二个*:任意名称 (..):任意参数 相当于所有以public开头的方法加了一个前置通知的话都会执行 --匹配指定包下所有类方法 execution(* top.odliken.demo.(..)) 不包含子包 .:包 --execution(* top.odliken.demo..(..)) ..便是包、子酸包下所有类 --匹配指定类所有方法 execution( top.odlien.demo.UserService.(..)) 第一个*:任意返回值 UserService.:UserService类下面的所有方法 --匹配实现特定接口所有方法 execution( top.odliken.demo.GenericDao+.(..)) +:子类 .:方法名 --匹配所有save开头的方法 execution(* save*(..))

入门案列

============XML==========
<!--配置目标类=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
===========ProductDao===========
public class ProductDao {
    public void save() {
        System.out.println("保存商品.....");
    }
    public void findOne() {
        System.out.println("查找一个商品.....");
    }
    public void findAll() {
        System.out.println("查找所有商品.....");
    }
    public void update() {
        System.out.println("修改商品.....");
    }
    public void delete() {
        System.out.println("删除商品.....");
    }
}
===========MyAspectAnno===========
@Aspect
public class MyAspectAnno {
    @Before(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
    public void before(){
        System.out.println("=========前置通知=========");
    }
}
===========Springdemo1===========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:afterglow.xml")
public class Springdemo1 {
    @Resource(name = "productDao")
    private ProductDao productDao;
    @Test
    public void demo1(){
        productDao.save();
        productDao.findOne();
        productDao.findAll();
        productDao.update();
        productDao.delete();
    }
}

@Before前置通知

public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}

@AfterReturning后置通知

@AfterReturning(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.update(..))",returning = "result")
public void afterReturing(Object result){
    System.out.println("==========后置通知=========="+result);
}

@Around环绕通知

@Around(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.delete(..)))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("环绕通知======前");
    Object obj = joinPoint.proceed();//执行目标方法 不调用就不执行
    System.out.println("环绕通知======后");
    return obj;
}

@AfterThrowing 异常抛出通知

@AfterThrowing(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findOne(..)))",throwing = "e")
public void  afterThrowing(Throwable e){
    System.out.println("==========异常通知=========="+e.getMessage());
}

@After 最终通知

@After(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findAll(..))")
public void after(){
    System.out.println("==========最终通知==========");
}

通过@Pointcut为切点命名

@Before(value = "myPointcut1()")
public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}
@Pointcut(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
private void myPointcut1(){}

AspectJ的XML方式的AOP开发

==========CustomerDao==========
public interface CustomerDao {
    public void save();
    public String update();
    public void delete();
    public void findOne();
    public void findAll();
}
==========CustomerDaoImpl==========
public class CustomerDaoImpl implements CustomerDao {
    public void save() {
        System.out.println("保存客户...");
    }
    public String update() {
        System.out.println("修改客户...");
        return "spring";
    }
    public void delete() {
        System.out.println("删除客户...");
    }
    public void findOne() {
        System.out.println("查询一个客户...");
//       int a = 1/ 0;
    }
    public void findAll() {
        System.out.println("查询多个客户...");
//        int b = 1/0;
    }
}
==========MyAspectXml==========
public class MyAspectXml {
    // 前置通知
    public void before(JoinPoint joinPoint) {
        System.out.println("XML方式的前置通知==============" + joinPoint);
    }
    // 后置通知
    public void afterReturing(Object result) {
        System.out.println("XML方式的后置通知==============" + result);
    }
    // 环绕通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("XML方式的环绕前通知==============");
        Object obj = joinPoint.proceed();
        System.out.println("XML方式的环绕后通知==============");
        return obj;
    }
    // 异常抛出通知
    public void afterThrowing(Throwable e) {
        System.out.println("XML方式的异常抛出通知=============" + e.getMessage());
    }
    // 最终通知
    public void after() {
        System.out.println("XML方式的最终通知=================");
    }
}
==========SpringDemo2==========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class SpringDemo2 {
    @Resource(name="customerDao")
    private CustomerDao customerDao;
    @Test
    public void demo1(){
        customerDao.save();
        customerDao.update();
        customerDao.delete();
        customerDao.findOne();
        customerDao.findAll();
    }
}
==========XML==========
<!--XML的配置方式完成AOP的开发===============-->
<!--配置目标类=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
<!--aop的相关配置=================-->
<aop:config>
    <!--配置切入点-->
    <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
    <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
    <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
    <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
    <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
    <!--配置AOP的切面-->
    <aop:aspect ref="myAspectXml">
        <!--配置前置通知-->
        <aop:before method="before" pointcut-ref="pointcut1"/>
        <!--配置后置通知-->
        <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
        <!--配置环绕通知-->
        <aop:around method="around" pointcut-ref="pointcut3"/>
        <!--配置异常抛出通知-->
        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
        <!--配置最终通知-->
        <aop:after method="after" pointcut-ref="pointcut5"/>
    </aop:aspect>
</aop:config>

加载全部内容

相关教程
猜你喜欢
用户评论