spring入门:关键点整理(3)-- aop

1:aop:面向切面编程

aop术语:

Joinpoint 连接点:原对象的方法

Pointcut 切入点:代理中已增强的方法

Advice 通知增强:增强的代码

Target 目标对象:被代理的对象,原对象

Waving 织入:通知应用到切入点的过程

proxy 代理:织入目标对象后,形成代理对象

aspect 切面:切入点+通知。通常为切面类

通知对象:

前置通知 before:目标方法之前加的代码

后置通知 After:目标方法之后加的代码。无论是否出现异常都会调用。相当于python异常try代码块中的finally

后置返回通知 AfterRetruning:目标方法之后的代码。出现异常不调用。相当于python异常try代码块中的else

环绕通知 AroundReturning :在目标方法的前后调用

异常通知 After:在目标方法出现异常后调用

2:底层为代理模式

代理模式详见以前文章: 代理模式  https://blog.csdn.net/xinyuebaihe/article/details/104202951

3:使用:

分为配置方式与注解方式

示例目录结构

目录结构

lib

1)用配置方式

切面

package com.csdn.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component
public class MyAdvice {
    public void before() throws Throwable {
        System.out.println("这是前置通知");
    }
    public void after(){
        System.out.println("这是后置通知");
    }
    public void afterReturning(){
        System.out.println("这是没有异常的后置通知,相当于python中的else");
    }
    public void exception(){
        System.out.println("这是发生异常后的通知");
    }
    /**
     * 增强版的环绕通知。强大无比,可以替代其它通知
     * @param jp
     * @throws Throwable
     */
    public void around2(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("这是环绕通知前");
        try{
            //前置通知
            this.before();
            jp.proceed();

        }catch (Exception e){
            //异常通知
            this.exception();
        }finally {
            //后置通知
            this.after();
        }
        //无异常后置通知
        this.afterReturning();
        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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.csdn.*"/>
    <!--切面类,通知对象的bean-->
    <bean id = "myAdvice" class="com.csdn.advice.MyAdvice"/>
    <!--切面配置-->
    <aop:config>
        <!--切入点定义,目标方法,要增强哪些方法-->
        <!--第一个星号:返回值。第二个星号:匹配的实现类。第三个星号:任意方法。两个点:方法的属性-->
        <aop:pointcut id="pointcut" expression="execution(* com.csdn.service.impl.*ServiceImpl.*(..))"/>
        <!--切面引入-->
        <aop:aspect ref = "myAdvice">
            <!--不同类型的通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
            <aop:after-throwing method="exception" pointcut-ref="pointcut"/>
            <aop:around method="around2" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>


    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath*:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="user" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
</beans>

测试类

package com.csdn.test;

import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.SQLException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
    @Autowired
    private StudentService studentService;

    @org.junit.Test
    public void test01(){
//        List<Student> all = null;
//        try {
//            all = studentService.findAll();
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(all);
        studentService.add();
    }
}

2)用注解方式

切面

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--包扫描。会生成bean-->
    <context:component-scan base-package="com.csdn.*"/>

    <!--配置自动切面生成。一定要有,要不然只有切面类注解也无效-->
    <aop:aspectj-autoproxy/>

    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath*:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="user" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
</beans>

配置文件

package com.csdn.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice2 {
    @Pointcut("execution(* com.csdn.service.impl.*ServiceImpl2.*(..))")
    public void pointcut(){
        
    }
    
    @Before("MyAdvice2.pointcut()")
    public void before() throws Throwable {
        System.out.println("这是前置通知");
    }
    @After("MyAdvice2.pointcut()")
    public void after(){
        System.out.println("这是后置通知");
    }

    @AfterReturning("MyAdvice2.pointcut()")
    public void afterReturning(){
        System.out.println("这是没有异常的后置通知,相当于python中的else");
    }

    @AfterThrowing("MyAdvice2.pointcut()")
    public void exception(){
        System.out.println("这是发生异常后的通知");
    }
    /**
     * 增强版的环绕通知。强大无比,可以替代其它通知
     * @param jp
     * @throws Throwable
     */
    @Around("MyAdvice2.pointcut()")
    public void around2(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("这是环绕通知前");
        try{
            //前置通知
            this.before();
            jp.proceed();

        }catch (Exception e){
            //异常通知
            this.exception();
        }finally {
            //后置通知
            this.after();
        }
        //无异常后置通知
        this.afterReturning();
        System.out.println("这是环绕通知后");
    }

}

测试类

package com.csdn.test;

import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring2.xml")
public class Test2 {
    @Autowired()
    private StudentService studentService2;

    @org.junit.Test
    public void test01(){
//        List<Student> all = null;
//        try {
//            all = studentService.findAll();
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(all);
        studentService2.add();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值