Spring5 学习笔记8

目录

AOP操作(准备)

1.在spring框架中一般基于AspectJ实现AOP操作

2.基于AspecJ实现AOP操作

3.在项目工程中引入AOP相关依赖

4.切入点表达式

AOP操作(Aspectj注解)

1.创建类,在类里面定义方法

2.创建增强类(编写增强逻辑)

3.进行通知的配置

4.配置不同类型的通知

5.相同切入点抽取

6.增强优先级设置

7.完全注解开发

AOP操作(Aspectj配置文件)


AOP操作(准备)

1.在spring框架中一般基于AspectJ实现AOP操作

(1)什么是AspectJ

*AspecJ不是spring组成部分,独立AOP框架,一般AspectJ和Spring框架一起使用,进行AOP操作

2.基于AspecJ实现AOP操作

(1)基于xml配置文件

(2)基于注解实现(常使用)

3.在项目工程中引入AOP相关依赖

4.切入点表达式

(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强

(2)语法结构:

execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]))

举例1:对包com.wz.dao.BookDao类里面add方法进行增强

                execution(* com.wz.dao.BookDao.add(..))

                *表示任意修饰符

举例2:对包com.wz.dao.BookDao类里面所有方法进行增强

                execution(* com.wz.dao.BookDao.*(..))

举例3:对包com.wz.dao包里面所有类,类里面所有方法进行增强

                excution(* com.wz.dao.*.*(..))

AOP操作(Aspectj注解)

1.创建类,在类里面定义方法

//被增强类
public class User {
    public void add(){
        System.out.println("add...");
    }
}

2.创建增强类(编写增强逻辑)

(1)在增强类里面,创建方法,让不同方法代表不同通知类型

//增强类
public class UserProxy {

    //前置通知
    public void before(){
        System.out.println("before...");
    }
}

3.进行通知的配置

(1)在spring配置文件中,开启注解扫描

<?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.wz.spring5.collectiontype.aopanno"></context:component-scan>
</beans>

(2)使用注解创建User和UserProxy对象

//被增强类
@Component
public class User {
    public void add(){
        System.out.println("add...");
    }
}
//增强类
@Component
public class UserProxy {

    //前置通知
    public void before(){
        System.out.println("before...");
    }
}

(3)在增强类上面,添加注解@Aspect

//增强类
@Component
@Aspect//生成代理对象
public class UserProxy {

    //前置通知
    public void before(){

        System.out.println("before...");
    }
}

(4)在spring配置文件中,开启生成代理对象

<?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.wz.spring5.aopanno"></context:component-scan>

    <!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.配置不同类型的通知

(1)在增强类的里面,在作为通知的方法上面添加通知类型的注解,使用切入点表达式配置

//增强类
@Component
@Aspect//生成代理对象
public class UserProxy {

    //前置通知
    //before注解 作为前置通知
    @Before(value = "execution(* com.wz.spring5.aopanno.User.add(..))")
    public void before(){

        System.out.println("before...");
    }
}

(2)编写测试代码,进行测试

package com.wz.spring5.test;

import com.wz.spring5.aopanno.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    @Test
    public void testAopAnno(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }
}

测试结果:

(3)其它类型通知

*@After 执行之后执行

*@AfterRerurning 返回值之后执行  异常不执行

*@AfterThrowing 出现异常执行 正常不执行

*@Around 环绕通知    环绕之后 异常时不执行

    @Around(value = "execution(* com.wz.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前");

        //被增强的方法执行
        proceedingJoinPoint.proceed();

        System.out.println("环绕之后...");
    }

5.相同切入点抽取

    //相同切入点抽取
    @Pointcut(value = "execution(* com.wz.spring5.aopanno.User.add(..))")
    public void point(){

    }

    //前置通知
    //before注解 作为前置通知
    @Before(value = "point()")
    public void before(){

        System.out.println("before...");
    }

6.增强优先级设置

有多个增强类对同一个方法增强,设置增强类优先级

(1)在增强类的上面添加@Order(数字类型值)注解,数字越小,优先级越高

@Component
@Aspect
@Order(1)
public class PersonProxy {

    @Before(value = "execution(* com.wz.spring5.aopanno.User.add(..))")
    public void around(){
        System.out.println("Person Before...");
    }
}

7.完全注解开发

创建配置类,不需要xml配置文件

@Configuration
@ComponentScan(basePackages = {"com.wz.spring5"})
@EnableAspectJAutoProxy(proxyTargetClass = true) //默认为true
public class ConfigAop {
}

AOP操作(Aspectj配置文件)

1.创建两个类,一个增强类,一个被增强类,创建方法

2.在spring配置文件中创建两个类对象

<?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 id="book" class="com.wz.spring5.Aopxml.Book"></bean>
    <bean id="bookProxy" class="com.wz.spring5.Aopxml.BookProxy"></bean>
</beans>

3.在spring配置文件中配置切入点

<?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 id="book" class="com.wz.spring5.Aopxml.Book"></bean>
    <bean id="bookProxy" class="com.wz.spring5.Aopxml.BookProxy"></bean>

    <!--配置AOP增强-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="b" expression="execution(* com.wz.spring5.Aopxml.Book.buy())"/>

        <!--配置切面-->
        <aop:aspect ref="bookProxy">
            <!--配置增强作用在具体方法上-->
            <aop:before method="before" pointcut-ref="b"></aop:before>
        </aop:aspect>

    </aop:config>
</beans>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值