坦克大战(一期)-设计模式-SpringAop

DesignPatterns
bingor 2 years ago
parent 2cfe9813bd
commit cc02d545ff

@ -16,6 +16,12 @@
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>

@ -0,0 +1,23 @@
package com.msb.proxy.springAop.v1;/**
* @Author bingor
* @Date 2022/10/17 14:45
* @Description: com.msb.proxy.springAop.v1
* @Version: 1.0
*/
/**
*@ClassName LogProxy
*@Description TODO
*@Author bingor
*@Date 2022/10/17 14:45
*@Version 3.0
*/
public class LogProxy {
public void before() {
System.out.println("method for before execute...");
}
public void after() {
System.out.println("method for after execute...");
}
}

@ -0,0 +1,24 @@
package com.msb.proxy.springAop.v1;/**
* @Author bingor
* @Date 2022/10/17 14:54
* @Description: com.msb.proxy.springAop.v1
* @Version: 1.0
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/17 14:54
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Tank tank = (Tank) context.getBean("tank");
tank.move();
}
}

@ -0,0 +1,29 @@
package com.msb.proxy.springAop.v1;/**
* @Author bingor
* @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
import com.msb.proxy.cglib.LogMethodInterceptor;
import org.springframework.cglib.proxy.Enhancer;
import java.util.Random;
/**
*@ClassName Tank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:41
*@Version 3.0
*/
public class Tank {
public void move() {
System.out.println("tank move……");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,31 @@
package com.msb.proxy.springAop.v2;/**
* @Author bingor
* @Date 2022/10/17 14:45
* @Description: com.msb.proxy.springAop.v1
* @Version: 1.0
*/
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
*@ClassName LogProxy
*@Description TODO
*@Author bingor
*@Date 2022/10/17 14:45
*@Version 3.0
*/
@Aspect
public class LogProxy {
@Before("execution(void com.msb.proxy.springAop.v2.Tank.move())")
public void before() {
System.out.println("auto:method for before execute...");
}
@After("execution(void com.msb.proxy.springAop.v2.Tank.move())")
public void after() {
System.out.println("auto:method for after execute...");
}
}

@ -0,0 +1,24 @@
package com.msb.proxy.springAop.v2;/**
* @Author bingor
* @Date 2022/10/17 14:54
* @Description: com.msb.proxy.springAop.v1
* @Version: 1.0
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/17 14:54
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-auto.xml"});
Tank tank = (Tank) context.getBean("tank");
tank.move();
}
}

@ -0,0 +1,26 @@
package com.msb.proxy.springAop.v2;/**
* @Author bingor
* @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
import java.util.Random;
/**
*@ClassName Tank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:41
*@Version 3.0
*/
public class Tank {
public void move() {
System.out.println("auto: tank move……");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,11 @@
<?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 />
<!-- test spring AOP -->
<bean id="tank" class="com.msb.proxy.springAop.v2.Tank"></bean>
<bean id="logProxy" class="com.msb.proxy.springAop.v2.LogProxy"></bean>
</beans>

@ -1,7 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
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">
<!-- test spring IOC -->
<bean id="driver" class="com.msb.springIOC.Driver"></bean>
<!-- test spring AOP -->
<bean id="tank" class="com.msb.proxy.springAop.v1.Tank"></bean>
<bean id="logProxy" class="com.msb.proxy.springAop.v1.LogProxy"></bean>
<aop:config>
<aop:aspect id="log" ref="logProxy">
<aop:pointcut id="onmove" expression="execution(void com.msb.proxy.springAop.v1.Tank.move())"/>
<aop:before method="before" pointcut-ref="onmove" />
<aop:after method="after" pointcut-ref="onmove" />
</aop:aspect>
</aop:config>
</beans>
Loading…
Cancel
Save