静态代理和动态代理,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>