码迷,mamicode.com
首页 > 其他好文 > 详细

基于子类的动态代理

时间:2020-07-12 11:51:05      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:code   param   mpi   ble   override   odi   depend   main   @param   

技术图片

 

 

<?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);
    }
}

 

基于子类的动态代理

标签:code   param   mpi   ble   override   odi   depend   main   @param   

原文地址:https://www.cnblogs.com/abuduri/p/13287699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!