如何解决在 Spring Webflux 中注入中间件
我正在尝试处理项目/存储库中的所有横切问题,例如异常、授权等,并注入多个独立且独立的 spring-webflux 项目,即通过构建可重用的微服务平台来处理横切问题。
谁能建议如何实现这一点?
例如:- light4j 处理所有横切问题,因为中间件只需要添加为插件。但它与 SpringWebflux 不兼容。 即使使用 AspectJ,我们也不能对不同的项目使用相同的处理程序,除非它们在同一个父项目下。
我尝试使用 AspectJ 的加载时编织功能。我在不同的项目中定义了方面,并在当前项目中添加了一个插件(我想在其中使用)但是当发生异常时,aspectJ 部分没有被调用
下面是我当前项目的 pom.xml
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dummy</groupId>
<artifactId>dummydmanagement</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DummyManagement</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</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>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>com.jaway.blog.aspectj</groupId>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>/home/mmt8281/codebase/annotations-element-value-pair-without-main-class/target/annotations-element-value-pair-without-main-class-1.0-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
下面是aspect项目的pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jaway.blog.aspectj</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
附加 gitHub 链接:-
解决方法
“复制和粘贴编程”的问题在于你使用了你不理解的东西。或许还是先学点基础比较好。
话虽如此,鉴于您从博客文章中复制的方面模块是原生 AspectJ,有两种方法可以解决您的问题:
-
通过后编译二进制编织或加载时编织,将所有想要使用方面的应用程序配置为也使用原生 AspectJ。 Spring 手册描述了 how to use LTW。
-
简单地将方面模块转换为 Spring 组件,并使所有客户端项目都使用 Spring AOP。
后者是我所做的,因为我想你只想在 Spring 项目中使用方面,并且你只在 Spring 组件中注释公共方法。
本机 AspectJ 解决方案会更强大,因为它也适用于静态和受保护/私有方法,即使对于非 Spring 代码,甚至完全在 Spring 之外。但是如果手枪就够了,为什么还要用大炮呢?
我向您发送了两个拉取请求:
您需要在方面模块中更改的内容基本上是:
- 删除 AspectJ Maven 插件
- 添加
org.springframework:spring-context
以便能够将@Component
添加到方面类。 - 我还优化了一些其他内容,例如更新 AspectJ 版本。
然后,在应用模块中,您需要:
- 添加
org.aspectj:aspectjweaver
- 将方面模块的基本包名称添加到组件扫描中
- 我还删除了丑陋的系统依赖项,其中包含指向 aspect 模块的固定路径。如果您在构建应用程序之前简单地在方面模块上运行
mvn clean install
,将在本地 Maven 存储库中找到该库。 (我认为,您需要紧急学习一些 Maven 基础知识。)
现在运行这个应用程序
@SpringBootApplication
@ComponentScan(basePackages = {"com.dummy.dummydmanagement","com.jayway.blog"})
public class DummyManagementApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(DummyManagementApplication.class,args);
context.getBean(ItemService.class).getAllItemsService();
}
// (...)
}
控制台日志将是:
YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.
Exception occured
YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.
当然,注解静态 main 方法对 Spring AOP 不起作用,这也是我去掉注解的原因。这仅适用于本机 AspectJ。如果您需要,请告诉我,但我认为您应该保持简单,因为您显然不了解您正在使用的工具。
更新:因为无论出于何种原因 OP 现在生我的气,因为我告诉他不要直接在 Telegram 上给我发短信,他还删除了自己的存储库。对于任何感兴趣的人,这里是我的克隆,包含原始代码和我解决问题的修改,如上所述:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。