Spring 基于Aspect的AOP

a450cc98bd9d9bc8d898908efe3c45de835.jpg

3f16584b9a69dd1334e438ced4407e026d5.jpg

dae94d18352f4cdf97a73a931798cf5c9e7.jpg

26ffa022d623f13c69854729f4d19b03725.jpg

0038e8cbcd6adc8570cabb4961369a78982.jpg

简单版:

ffa60fdad939744cc6e5305254ad31d503f.jpg

完整版例子:

package com.imooc.aspectJ.demo1;

public class ProductDao {

    public void save(){
        System.out.println("保存商品...");
    }

    public String update(){
        System.out.println("修改商品...");
        return "hello";
    }

    public void delete(){
        System.out.println("删除商品...");
    }

    public void findOne()  {
        System.out.println("查询一个商品...");
//        int i=1/0;
    }

    public void findAll(){
        System.out.println("查询所有商品...");
//        int i=1/0;
    }
}
package com.imooc.aspectJ.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 切面类
 */
@Aspect
public class MyAspectAnno {

    @Before(value = "MyPointCut1()||MyPointCut2()")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知=================="+joinPoint);
    }

    @AfterReturning(value = "MyPointCut2()",returning="result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        System.out.println("后置通知============="+joinPoint+",result:"+result);
    }

    @Around(value = "MyPointCut3()")
    public Object around(ProceedingJoinPoint joinPoint) throws  Throwable{
        System.out.println("=======环绕前通知===========");
        Object obj = joinPoint.proceed();
        System.out.println("=======环绕后通知===========");
        return obj;
    }

    @AfterThrowing(value="MyPointCut4()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常抛出通知==============="+e.getMessage());
    }

    @After(value = "MyPointCut5()")
    public void after(){
        System.out.println("最终通知==========");
    }

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))))")
    private void MyPointCut1(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))))")
    private void MyPointCut2(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))))")
    private void MyPointCut3(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))))")
    private void MyPointCut4(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))))")
    private void MyPointCut5(){}

}
<?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 -->

    <!--开启AspectJ的注解开发,自动代理=========-->
    <aop:aspectj-autoproxy/>

    <!--目标类================================-->
    <bean id="productDao" class="com.imooc.aspectJ.demo1.ProductDao"/>

    <!--定义切面-->
    <bean class="com.imooc.aspectJ.demo1.MyAspectAnno"/>
</beans>
package com.imooc.aspectJ.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {

    @Resource(name="productDao")
    private ProductDao productDao;

    @Test
    public void demo1(){
        productDao.save();
        productDao.update();
        productDao.delete();
        productDao.findAll();
        productDao.findOne();
    }
}

 

d103f3cfd77153256326bccc706ef2106ca.jpg

5861cbf776253e5250381e256a5689ad89e.jpg

97dc4a9991d809a135aa442d6b35aed5b7e.jpg

e5124bd6650ca6804644c3e8fbe37845688.jpg

4ea8354d651900d7c1ef9d038d263dea841.jpg

3d3352e27ff8a156fa2d04f9046dd38c34a.jpg

172728cd887d3904a587e8e09f241cb79c3.jpg

 

===============XML方式===============

package com.imooc.aspectJ.demo2;

public interface CustomerDao {
    public void save();
    public String update();
    public void delete();
    public void findOne();
    public void findAll();
}
package com.imooc.aspectJ.demo2;

public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void save() {
        System.out.println("保存客户...");
    }

    @Override
    public String update() {
        System.out.println("修改客户...");
        return "spring";
    }

    @Override
    public void delete() {
        System.out.println("删除客户...");
    }

    @Override
    public void findOne() {
        System.out.println("查询一个客户...");
        int i=1/0;
    }

    @Override
    public void findAll() {
        System.out.println("查询多个客户...");
        //int i=1/0;
    }
}
package com.imooc.aspectJ.demo2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspectXml {

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

    //后置通知
    public void afterRetruning(String 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方式的最终通知通知=============");
    }
}
package com.imooc.aspectJ.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@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 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 -->

  <!--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="afterRetruning" 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>

</beans>

一般在实际开发过程中AOP,基本不会使用传统AOP开发。一般使用AspectJ的方式。企业中有些用注解,有些使用XML方式。

转载于:https://my.oschina.net/popfei/blog/1830787

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值