基于子类的动态代理

Posted abuduri

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于子类的动态代理相关的知识,希望对你有一定的参考价值。

技术图片

 

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>proxy</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>14</java.version>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
        <encoding>UTF-8</encoding>
    </properties>
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>
package com.example.cglib;

/**
 * 消费者
 */
public class Client {
    public static void main(String[] args) {
        Producer producer=Proxy2.getProxy();
        producer.saleProduct(10000);

    }
}
package com.example.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class Proxy2 {

    public static Producer getProxy(){
        Producer t=(Producer) Enhancer.create(Producer.class, new MethodInterceptor() {
            Producer p = new Producer();
            /**
             *
             * @param o==proxy
             * @param method==method
             * @param objects==args
             * @param methodProxy:当前执行方法的代理对象
             * @return
             * @throws Throwable
             */
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                if("saleProduct".equals(method.getName())){
                    objects[0]=0.8f*(float)objects[0];
                }
                return method.invoke(p,objects);
            }
        });
        return t;
    }
}
package com.example.cglib;

/**
 * 生产者
 */
public class Producer {
    /**
     * 销售
     * @param money
     */
    public void saleProduct(float money){
        System.out.println("销售产品,并拿到钱:"+money);
    }

    /**
     * 销后
     * @param money
     */
    public void afterService(float money){
        System.out.println("销后服务,并拿到钱:"+money);
    }
}

 

以上是关于基于子类的动态代理的主要内容,如果未能解决你的问题,请参考以下文章

基于接口和基于子类的动态代理

基于子类的动态代理

基于子类的动态代理

Spring的AOP总结

Spring_Aop的xml和注解的使用

Spring------AOP