侧边栏壁纸
  • 累计撰写 101 篇文章
  • 累计创建 89 个标签
  • 累计收到 9 条评论

spring系列笔记 - 第⼗五章 Spring的动态代理开发

bearjun
2021-07-06 / 0 评论 / 0 点赞 / 941 阅读 / 3,256 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-07-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Spring动态代理的概念

概念:通过代理类为原始类(目标类)增加额外功能
好处:利于原始类(目标类)的维护

spring动态代理开发步骤

1、引入相关的jar包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.14.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.8</version>
</dependency>

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

2、创建原始对象(并在配置文件中设置)

// 接口
public interface UserService {
    void register(User user);
    boolean login(String name, String password);
}

// 接口的实现
public class UserServiceImpl implements UserService {
    @Override
    public void register(User user) {
        System.out.println("UserServiceImpl.register 业务运算 + DAO");
    }

    @Override
    public boolean login(String name, String password) {
        System.out.println("UserServiceImpl.login 业务运算 + DAO");
        return true;
    }
}

// xml的配置文件
<bean id="userService" class="com.bearjun.proxy.UserService" />

3、额外功能(MethodBeforeAdvance接口)
额外的功能写在该接口的实现中,在方式方法执行前执行接口中的方法。

//把运行原始方法执行前的运行的额外功能,书写在before方法中
public class Before implements MethodBeforeAdvice {
    /**
     * 作用: 把需要运行在原始方法执行之前运行的额外功能, 书写在 before 方法中
     */
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("hhhhh");
    }
}

// 配置额外功能
<bean id="before" class="com.bearjun.aop.Before"/>

4、定义切入点

切入点:额外功能加入的位置
目的:根据自己的需求,决定额外功能加入给哪个原始方法

<!--⽬的: 由程序员根据⾃⼰的需要,决定额外功能加入给哪个原始方法(register、login)-->
<!-- 简单的测试:所有方法都做为切入点,都加入额外的功能-->
<aop:config>
    <aop:pointcut id="pc" expression="execution(* * (..))"/>
</aop:config>

5、组装(步骤3,4整合)

<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
                           https://www.springframework.org/schema/aop/spring-aop.xsd">
	
    <bean id="userService" class="com.yusael.aop.UserServiceImpl"/>
    <!-- 额外功能 -->
    <bean id="before" class="com.yusael.aop.Before"/>
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* * (..))"/>
        <!--表达的含义: 所有的方法 都加入before的额外功能-->
        <aop:advisor advice-ref="before" pointcut-ref="pc"/>
    </aop:config>

</beans>
调用测试
/**
 * 用于测试动态代理
 */
@Test
public void test1() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    UserService userService = (UserService) ctx.getBean("userService");
    userService.login("admin", "1234");
    userService.register(new User());
}

注意:

  • Spring 的工厂通过原始对象的 id 值获得的是代理对象
  • 获得代理对象后,可以通过声明接口类型,进行对象的存储

动态代理细节分析

1、Spring创建的动态代理类在哪里?

Spring框架在运行时,通过动态字节码技术,在JVM创建的,运行在JVM内部,等程序结束后,会和虚拟机一起消失。

什么是动态字节码技术:通过第三方动态字节码框架,在虚拟机中创建对应类的字节码,进而创建对象,当虚拟机结束,动态字节码跟着消失
40138-93f77tyebao.png
**结论:**动态代理不需要创建类文件,都是Jvm在运行过程中创建的,所以不会造成静态代理类文件数量过多,影响项目管理问题。

2、动态代理模式简化代理的开发

在额外功能不改变的前提下,创建其他目标类(原始类)的代理对象时,只需要指定原始(目标)对象即可。

3、动态代理额外功能的维护性大大增强

打开扩展,关闭修改(开闭原则)

0

评论区