JavaWeb day23-Spring-AOP1

AOP是什么?

AOP(Aspect Oriented Programming)是面向切面编程。
简单说 就是在不改变方法原代码的基础上,对方法进行功能增强

动态代理

代理:指为一个目标对象提供一个代理对象, 并由代理对象控制对目标对象的引用. 使用代理对象, 是为了在不修改目标对象的基础上, 增强目标对象的业务逻辑.

动态代理:静态代理会为每一个业务增强都提供一个代理类, 由代理类来创建代理对象, 而动态代理并不存在代理类, 代理对象直接由代理生成工具动态生成.

AOP对程序的扩展方式采用动态代理的方式. (JDK动态代理和Cglib动态代理两种方式)

动态代理JDK proxy

  1. 动态代理,必须要有一个接口 com.wzx.demo01
  2. 动态代理的三个参数代理类,与被代理类 地位是一样的 (相同的ClassLoader,相同的接口) 所以,有相同的接口,相同的类加载器
  3. invoke方法
    参2 表示要增强哪个方法
    Object returnValue = method.invoke(XX,args);

添加项目依赖

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

    </dependencies>

项目结构

在这里插入图片描述

接口类IUser

public interface IUser {
    void work();
}

实现类UserImpl

public class UserImpl implements IUser {

    @Override
    public void work() {
        System.out.println("aaaaaaaa");
    }
}

增强类Advice

package com.zy.service;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

public class Advice {
    public void writelog(){
        System.out.println("备忘录");

    }
}

用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: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">

<!--创建UserImpl对象 目标类-->
    <bean id="userImpl" class = "com.zy.service.impl.UserImpl"/>
<!--    创建Advice对象-->
    <bean id="advice" class="com.zy.service.Advice"/>
<!--    配置增强关系,生成代理类-->
<!--    配切点-->
    <aop:config>
<!--        定位-->
        <aop:pointcut id="work" expression="execution(public void com.zy.service.IUser.work())"/>
<!--        写逻辑:想在work方法之后调writelog-->
        <aop:aspect ref="advice">
        <!--        writelog靠ref指定,work靠expression指定-->
            <aop:after method="writelog" pointcut-ref="work"/>
        </aop:aspect>

    </aop:config>

</beans>

单元测试

@RunWith(SpringJUnit4ClassRunner.class)
//先读xml文件
@ContextConfiguration("classpath:applicationContext.xml")
public class UserImplTest {
//    取文件
    @Autowired
    IUser iUser;

    @Test
    public void work() {
        System.out.println(iUser);
        iUser.work();
    }
}

结果

根据此语句可知<aop:after method=“writelog” pointcut-ref=“work”/> 在work之后指定下一个语句的输出,即writelog
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值