首页 > 其他分享 >10_Spring_CGLIB动态代理

10_Spring_CGLIB动态代理

时间:2023-07-30 12:44:21浏览次数:41  
标签:10 Spring Object 代理 Person CGLIB import 父类 eat

10_Spring_CGLIB动态代理

proxy 动态代理

面向接口

1必须有接口和实现类

2增强接口中定义的方法

3只能读取接口中方法的上注解

cglib动态代理模式

面向父类

image

  1. package com.msb.testCglib;
  2. import org.junit.Test;
  3. import org.springframework.cglib.proxy.Enhancer;
  4. import org.springframework.cglib.proxy.MethodInterceptor;
  5. import org.springframework.cglib.proxy.MethodProxy;
  6. import java.lang.reflect.Method;
  7. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  8. */
  9. public class Test1 {
  10. @Test
    
  11. public void testCglib(){
    
  12.     Person person =new Person();
    
  13.     // 获取一个Person的代理对象
    
  14.     // 1 获得一个Enhancer对象
    
  15.     Enhancer enhancer=new Enhancer();
    
  16.     // 2 设置父类字节码
    
  17.     enhancer.setSuperclass(person.getClass());
    
  18.     // 3 获取MethodIntercepter对象 用于定义增强规则
    
  19.     MethodInterceptor methodInterceptor=new MethodInterceptor() {
    
  20.         @Override
    
  21.         public Object intercept(Object o, Method method, Object[]
    
    objects, MethodProxy methodProxy) throws Throwable {
  22.             /*Object o,  生成之后的代理对象 personProxy
    
  23.             Method method,  父类中原本要执行的方法  Person>>> eat()
    
  24.             Object[] objects, 方法在调用时传入的实参数组
    
  25.             MethodProxy methodProxy  子类中重写父类的方法 personProxy >>> eat()
    
  26.             */
    
  27.             Object res =null;
    
  28.             if(method.getName().equals("eat")){
    
  29.                 // 如果是eat方法 则增强并运行
    
  30.                 System.out.println("饭前洗手");
    
  31.                 res=methodProxy.invokeSuper(o,objects);
    
  32.                 System.out.println("饭后刷碗");
    
  33.             }else{
    
  34.                 // 如果是其他方法 不增强运行
    
  35.                 res=methodProxy.invokeSuper(o,objects); //
    
    子类对象方法在执行,默认会调用父类对应被重写的方法
  36.             }
    
  37.             return res;
    
  38.         }
    
  39.     };
    
  40.     // 4 设置methodInterceptor
    
  41.     enhancer.setCallback(methodInterceptor);
    
  42.     // 5 获得代理对象
    
  43.     Person personProxy = (Person)enhancer.create();
    
  44.     // 6 使用代理对象完成功能
    
  45.     personProxy.eat("包子");
    
  46. }
    
  47. }
  48. class Person {
  49. public Person( ) {
    
  50. }
    
  51. public void eat(String foodName) {
    
  52.     System.out.println("张三正在吃"+foodName);
    
  53. }
    
  54. }

Generated with Mybase Desktop 8.2.13

标签:10,Spring,Object,代理,Person,CGLIB,import,父类,eat
From: https://www.cnblogs.com/01way/p/17591280.html

相关文章

  • 14_Spring_JDBCTemplate的使用
    14_Spring_JDBCTemplate的使用JdbcTemplate概述JdbcTemplate是spring框架中提供的一个对象,是对原始繁琐的JdbcAPI对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等......
  • c语言之判断100-200内的素数
    intmain()//判断100-200内的素数{ //判断素数,即只能被1和他自身整除 //1.试除法 //假设13为素数,就拿2-12的数来试着整除,若可以那就不是素数,若不可以就是素数 //由此可知:如果2到i-1的数可以被i给整除,那么i就不是素数 inti=0; intcount=0; for(i=100;i<=200;i+......
  • 1_Spring概念介绍_了解
    1_Spring概念介绍_了解Spring是分层的全栈式的轻量级开发框架,以IOC和AOP为核心,官网是https://spring.ioSpring版本情况Spring优势1方便解耦,简化开发Spring通过容器,将对象的创建从代码中剥离出来,交给Spring控制,避免直接编码造成模块之间的耦合度高,用户也不必自己编......
  • Soft Rasterizer (SoftRas) windows10 vscode 运行 环境配置记录
    配了好几天,一边搜索一边配,记录下做了些什么。我的环境:windows10,vscode,RTX3090配置cl环境变量安装openssl,配置环境变量安装libtorch,配置环境变量打开对应的虚拟环境里面的打开torch\include\ATen\Parallel.h,将inlineTORCHAPIvoidlazyinitnumthreads()的实现......
  • Sqli-labs靶场之SQL手注通关详解(Less 1~10)
    Less-1 GET-Errorbased-Singlequotes-String判断注入点,这里的页面中没有可以注入的地方,因此可以判断注入点在url栏。判断注入类型id=1and1=1页面正常id=1and1=2页面正常id=1'and1=1--+页面正常id=1'and1=2--+ 页面正常由此可以判断该注入......
  • 【SpringBoot】快速入门
    (知识目录)一、SpringBoot快速入门1.1idea创建(1)新建模块,选择SpringInitializer,一定要记得选择SpringBoot的版本为2.7.7注意上面的两个目录一定要是空目录,然后点击apply,OK即可,之后进入到主界面。(2)编写UserController类packagecom.itxiaoguo.controller;importor......
  • Spring Security安全框架在Spring Boot框架中的使用
    SpringSecurity是一个基于Spring框架的安全框架,它提供了一系列的安全服务和功能,包括身份验证、授权、防护等。在SpringBoot框架中,SpringSecurity是一个非常重要的组件,它可以帮助我们实现应用程序的安全性。本文将详细介绍SpringSecurity在SpringBoot框架中的使用,包括如何配置S......
  • 撞了100次南墙才明白的道理
    撞了100次南墙才明白的道理:1、太正经的人,发不了大财。2、你越没本事,别人越欺负你。3、社会资源是有限的,好的资源需要靠抢。4、除了父母,没有人愿意无偿包容你。5、没有实力,你认识谁都没有用。6、人们只会看重你的成果,而不是你的努力。7、在竞争激烈的世界里,弱者只会被淘汰。......
  • springboot的controller如何拿到post请求中的json数据
    在SpringBoot的Controller中,可以使用注解@RequestBody来获取POST请求中的JSON数据。我们可以将这个注解应用到一个Controller方法的参数上,Spring将会负责读取请求正文中的数据,将其反序列化为一个Java对象,并将其作为Controller方法的参数传递。以Java代码为例,示例代码如下:@RestC......
  • 【230730-3】已知:2^a=5,2^b=10,2^c=80 求:2019a-4039b+2020c=?
    ......