更新案例
This commit is contained in:
parent
648ebe5708
commit
756c4f7381
34
.gitignore
vendored
Normal file
34
.gitignore
vendored
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
target/
|
||||||
|
**/target/**
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
16
README.md
16
README.md
@ -1,3 +1,17 @@
|
|||||||
# LoadTimeWeaving-demo
|
# LoadTimeWeaving-demo
|
||||||
|
|
||||||
LTW(LoadTimeWeaving)技术实现AOP功能
|
LTW(LoadTimeWeaving)技术实现AOP功能
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
参考了:
|
||||||
|
https://www.eclipse.org/aspectj/doc/released/devguide/ltw-configuration.html
|
||||||
|
|
||||||
|
执行步骤:
|
||||||
|
1.mvn clean package,得到jar包:java-aspectj-agent-1.0-SNAPSHOT
|
||||||
|
|
||||||
|
2.把aspectjweaver-1.8.2.jar拷贝到和本jar包同路径下
|
||||||
|
|
||||||
|
3.cmd下执行:
|
||||||
|
java -javaagent:aspectjweaver-1.8.2.jar -cp java-aspectj-agent-1.0-SNAPSHOT.jar cn.x47.ltw.Main
|
||||||
|
|
||||||
|
25
java-aspectj-agent/pom.xml
Normal file
25
java-aspectj-agent/pom.xml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.x47.blog</groupId>
|
||||||
|
<artifactId>LoadTimeWeaving-demo</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>java-aspectj-agent</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
19
java-aspectj-agent/src/main/java/cn/x47/ltw/Main.java
Normal file
19
java-aspectj-agent/src/main/java/cn/x47/ltw/Main.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package cn.x47.ltw;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuwei
|
||||||
|
*/
|
||||||
|
public final class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Map<String, String> map = new HashMap<>(1);
|
||||||
|
map.put("key", "ssss");
|
||||||
|
JSON.toJSONString(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package cn.x47.ltw;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuwei
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
public class ProfilingAspect {
|
||||||
|
|
||||||
|
@Pointcut("execution(public * com.alibaba.fastjson.JSON.toJSONString(java.lang.Object))")
|
||||||
|
public void p2() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around("p2()")
|
||||||
|
public Object profile2(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
System.out.println("A2");
|
||||||
|
Object proceed = pjp.proceed();
|
||||||
|
System.out.println(proceed);
|
||||||
|
System.out.println("q2");
|
||||||
|
return proceed;
|
||||||
|
}
|
||||||
|
}
|
15
java-aspectj-agent/src/main/resources/META-INF/aop.xml
Normal file
15
java-aspectj-agent/src/main/resources/META-INF/aop.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
|
||||||
|
<aspectj>
|
||||||
|
|
||||||
|
<weaver>
|
||||||
|
<!-- only weave classes in our application-specific packages -->
|
||||||
|
<include within="cn.x47.ltw..*"/>
|
||||||
|
<include within="com.alibaba.fastjson.*"/>
|
||||||
|
</weaver>
|
||||||
|
|
||||||
|
<aspects>
|
||||||
|
<!-- weave in just this aspect -->
|
||||||
|
<aspect name="cn.x47.ltw.ProfilingAspect"/>
|
||||||
|
</aspects>
|
||||||
|
|
||||||
|
</aspectj>
|
51
pom.xml
Normal file
51
pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?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>cn.x47.blog</groupId>
|
||||||
|
<artifactId>LoadTimeWeaving-demo</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>LoadTimeWeaving-demo</name>
|
||||||
|
<url>https://www.61dz.com</url>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.7.3</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>java-aspectj-agent</module>
|
||||||
|
<module>springboot-aspectj-agent</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.7.3</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>1.9.6</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>2.0.12</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
</project>
|
61
springboot-aspectj-agent/pom.xml
Normal file
61
springboot-aspectj-agent/pom.xml
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.x47.blog</groupId>
|
||||||
|
<artifactId>LoadTimeWeaving-demo</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>aspectj-agent-springboot</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>2.0.12</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>1.9.9</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,23 @@
|
|||||||
|
package cn.x47.ltw.springboot;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuwei
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class TestApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(TestApplication.class, args);
|
||||||
|
Map<String, String> map = new HashMap<>(1);
|
||||||
|
map.put("key", "ssss");
|
||||||
|
JSON.toJSONString(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package cn.x47.ltw.springboot.aop;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuwei
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
public class TestAspect {
|
||||||
|
|
||||||
|
@Around("execution(* com.alibaba.fastjson..*.*(..))")
|
||||||
|
public String parse2String(ProceedingJoinPoint join) {
|
||||||
|
System.out.println("parse to String before");
|
||||||
|
String str = "";
|
||||||
|
try {
|
||||||
|
str = (String) join.proceed();
|
||||||
|
System.out.println("result:" + str);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("parse to String after");
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package cn.x47.ltw.springboot.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuwei
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public class TestController {
|
||||||
|
@GetMapping
|
||||||
|
public Object test(String a) {
|
||||||
|
Map<String, String> map = new HashMap<>(1);
|
||||||
|
map.put("key", a);
|
||||||
|
return JSON.toJSONString(map);
|
||||||
|
}
|
||||||
|
}
|
12
springboot-aspectj-agent/src/main/resources/META-INF/aop.xml
Normal file
12
springboot-aspectj-agent/src/main/resources/META-INF/aop.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<aspectj>
|
||||||
|
<weaver options="-XnoInline -Xset:weaveJavaxPackages=true -Xlint:ignore -verbose -XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
|
||||||
|
<!--在编织时导入切面类和需要被切入的目标类-->
|
||||||
|
<include within="cn.x47.ltw..*"/>
|
||||||
|
<include within="com.alibaba.fastjson..*"/>
|
||||||
|
</weaver>
|
||||||
|
<aspects>
|
||||||
|
<!--指定切面类-->
|
||||||
|
<aspect name="cn.x47.ltw.springboot.aop.TestAspect"/>
|
||||||
|
</aspects>
|
||||||
|
</aspectj>
|
@ -0,0 +1,2 @@
|
|||||||
|
server:
|
||||||
|
port: 8080
|
@ -0,0 +1,13 @@
|
|||||||
|
package cn.x47.ltw.springboot;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class TestApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user