学习之路---java设计模式--代理模式Proxy

静态代理和动态代理,SpringAop

静态代理

package org.example.ms.proxy;

import java.util.Random;

/**
 * @author Gavin.luo
 * @title: StaticProxy
 * @projectName MyData
 * @description:静态代理
 * @date 2022/3/17 14:13
 */
public class StaticProxy {

    public static void main(String[] args) {
        Movble movble=new TankTimer(new Tank());
        movble.move();
        Movble movble2=new TankLoger(new Tank());
        movble2.move();
        System.out.println("----------------------");
        //嵌套使用
        new TankTimer(new TankLoger(new Tank())).move();
    }
}

interface Movble {
    void move();
}

class Tank implements Movble {

    @Override
    public void move() {
        System.out.println("坦克开始移动---》");
    }
}

class TankTimer implements Movble {
    Movble movble;

    public TankTimer(Movble movble) {
        this.movble = movble;
    }

    @Override
    public void move() {
        long start = System.currentTimeMillis();
        movble.move();
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

class TankLoger implements Movble {
    Movble movble;

    public TankLoger(Movble movble) {
        this.movble = movble;
    }

    @Override
    public void move() {
        System.out.println("开始");
        movble.move();
        System.out.println("结束");
    }
}

JDK动态代理

 

package org.example.ms.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Random;

/**
 * @author Gavin.luo
 * @title: Proxy
 * @projectName MyData
 * @description:
 * @date 2022/3/17 15:12
 */
public class Proxy {
    public static void main(String[] args) {
        Tank2 tank2=new Tank2();
        Movble2 movble2 = (Movble2)java.lang.reflect.Proxy.newProxyInstance(Tank2.class.getClassLoader(),new Class[]{Movble2.class},new TankHander(tank2));
        movble2.move();
    }
}

interface Movble2{
    void move();
}

class Tank2 implements Movble2{

    @Override
    public void move() {
        System.out.println("坦克移动-----》");
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class TankHander implements InvocationHandler{
    Tank2 tank2;

    public TankHander(Tank2 tank2) {
        this.tank2 = tank2;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("method ----->"+method.getName()+"----->start");
        Object o = method.invoke(tank2, args);
        System.out.println("method ----->"+method.getName()+"----->end");
        return o;
    }
}

SpringAOP

<dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
package org.example.ms.proxy.spring;

/**
 * @author Gavin.luo
 * @title: Tank
 * @projectName MyData
 * @description:
 * @date 2022/3/21 15:29
 */
public class Tank {

    public void move(){
        System.out.println("kakakakakakaka.............");
    }
}
package org.example.ms.proxy.spring;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author Gavin.luo
 * @title: TimerProxy
 * @projectName MyData
 * @description:
 * @date 2022/3/21 15:30
 */

@Aspect
public class TimerProxy {

    @Before("execution(void org.example.ms.proxy.spring.Tank.move())")
    void before(){
        System.out.println("before--->"+System.currentTimeMillis());
    }
    @After("execution(void org.example.ms.proxy.spring.Tank.move())")
    void after(){
        System.out.println("after--->"+System.currentTimeMillis());
    }
}
package org.example.ms.proxy.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Gavin.luo
 * @title: Test
 * @projectName MyData
 * @description:
 * @date 2022/3/21 15:27
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
        Tank tank = (Tank) app.getBean("tank");
        tank.move();
    }
}
<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" 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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">




    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="tank" class="org.example.ms.proxy.spring.Tank"></bean>

    <bean id="TimerProxy" class="org.example.ms.proxy.spring.TimerProxy"></bean>

</beans>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值