proxy pattern spring aop

master
terry 3 years ago
parent febdcd66fe
commit 8458a90031

@ -17,7 +17,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
@ -25,5 +25,12 @@
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,12 @@
package proxy.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("app_auto.xml");
Tank tank = (Tank)context.getBean("tank");
tank.move();
}
}

@ -0,0 +1,14 @@
package proxy.spring;
import java.util.Random;
public class Tank {
public void move(){
System.out.println("tank is moving...");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,19 @@
package proxy.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TimeProxy {
@Before("execution(void proxy.spring.Tank.move())")
public void before(){
System.out.println("start time: " + System.currentTimeMillis());
}
@After("execution(void proxy.spring.Tank.move())")
public void end(){
System.out.println("end time: " + System.currentTimeMillis());
}
}

@ -0,0 +1,12 @@
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<bean id="tank" class="proxy.spring.Tank"/>
<bean id="timeProxy" class="proxy.spring.TimeProxy"/>
</beans>
Loading…
Cancel
Save