更新案例

This commit is contained in:
2022-08-28 11:43:13 +08:00
parent 648ebe5708
commit 756c4f7381
14 changed files with 346 additions and 1 deletions

View 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>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View 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>

View File

@ -0,0 +1,2 @@
server:
port: 8080

View File

@ -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() {
}
}