gpt4 book ai didi

java - 如何使用加载时间编织在一个 jar 中创建 @Aspect 并拦截另一个 jar 中的方法?

转载 作者:行者123 更新时间:2023-12-02 09:26:58 27 4
gpt4 key购买 nike

我浏览了许多与方面相关的帖子,但似乎没有一个对我有帮助。我发现很多帖子都非常旧,并且当我使用多个 .jar 时,所有示例 GitHub 存储库都只包含一个 .jar。

我知道我的通用 jar 已正确包含,因为我可以看到其他代码清楚地工作。当我构建通用 jar 时,我会在输出日志中看到我的方面。

mvn clean install日志

-------------------------------------------------------
T E S T S
-------------------------------------------------------
[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.13 built on Wednesday Nov 15, 2017 at 19:26:44 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info using configuration /C:/Users/<username>/Documents/IntelliJ/Team-Common/team-common/target/classes/META-INF/aop.xml
[AppClassLoader@18b4aac2] info using configuration file:/C:/Users/<username>/.m2/repository/org/springframework/spring-aspects/4.3.13.RELEASE/spring-aspects-4.3.13.RELEASE.jar!/META-INF/aop.xml
[AppClassLoader@18b4aac2] info register aspect com.mycompany.myteam.common.aspect.MyAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect
[AppClassLoader@18b4aac2] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'org.springframework.cache.jcache.interceptor.JCacheAspectSupport' which cannot be found on the classpath
[AppClassLoader@18b4aac2] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath
[IsolatedClassLoader@48503868] info AspectJ Weaver Version 1.8.13 built on Wednesday Nov 15, 2017 at 19:26:44 GMT
[IsolatedClassLoader@48503868] info register classloader org.apache.maven.surefire.booter.IsolatedClassLoader@48503868
[IsolatedClassLoader@48503868] info using configuration /C:/Users/<username>/Documents/IntelliJ/Team-Common/team-common/target/classes/META-INF/aop.xml
[IsolatedClassLoader@48503868] info using configuration file:/C:/Users/<username>/.m2/repository/org/springframework/spring-aspects/4.3.13.RELEASE/spring-aspects-4.3.13.RELEASE.jar!/META-INF/aop.xml
[IsolatedClassLoader@48503868] info register aspect com.mycompany.myteam.common.aspect.MyAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[IsolatedClassLoader@48503868] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect
[IsolatedClassLoader@48503868] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'org.springframework.cache.jcache.interceptor.JCacheAspectSupport' which cannot be found on the classpath
[IsolatedClassLoader@48503868] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

当我在 UserSearch.jar 中调用 API 时,它会正确返回数据,但即使 Controller 方法与 Common Jar 内我的方面的切入点相匹配,方面也永远不会被触发。

为什么我的 Common Jar 方面没有拦截 UserSeach 应用程序?

这是我的应用程序结构。

普通 jar

package com.mycompany.myteam.common.aspect;

@Aspect
@Component
public class MyAspect {

private static final Logger LOGGER = LoggerFactory.getLogger(AuditAdvice.class);


@Around("execution(public * com.mycompany.myteam.*.*Controller.*(..)) "
+ "&& !execution(public * com.mycompany.myteam.*.AuditController.*(..))")
public Object aroundRestEndpoints(ProceedingJoinPoint pjp) throws Throwable {

System.err.println("\n\n---------------------------------INSIDE ASPECT---------------------------------\n\n");

return pjp.proceed();

}
}
package com.mycompany.myteam.common.config;

@Configuration
@EnableAspectJAutoProxy
public class CommonAppConfig {

// ... Unrelated
}

/src/main/resources/META-INF/aop.xml

<aspectj>
<aspects>
<aspect name="com.mycompany.myteam.common.aspect.MyAspect"/>
<weaver options="-verbose -showWeaveInfo">
<include within="com.mycompany.myteam.*"/>
</weaver>
</aspects>
</aspectj>

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myteam.common</groupId>
<artifactId>team-common</artifactId>
<version>1.0.0</version>
<name>team-common</name>
<description>team-common</description>


<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<packaging>jar</packaging>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Dependency Versions -->
<aspectj.version>1.8.13</aspectj.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

<dependencies>

<!-- AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>


<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>


<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>


<repositories>
<repository>
<id>central</id>
<name>libs-release</name>
<url>http://somerepo.com/artifacts/release</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>libs-snapshot</name>
<url>http://somerepo.com/artifacts/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>spring-milestones</name>
<url>http://somerepo.com/artifacts/milestones</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.version}</source>
<target>${java.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.version}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>-XX:-UseSplitVerifier</argLine>
<argLine>
-javaagent:"${settings.localRepository}"/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

UserSearch.jar

package com.mycompany.myteam.usersearch.controller;

@RestController
@RequestMapping("/usersearch")
public class UserSearchController {

@Autowired
private UserSearchService userSearchService;


@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
public List<UserDTO> search(@PathVariable("name") String name){
return userSearchService.search(name);
}
}
package com.mycompany.myteam.usersearch;

@SpringBootApplication
@ComponentScan("com.mycompany.myteam")
public class UserSearchApplication {

public static void main(String[] args) {
SpringApplication.run(UserSearchApplication.class, args);
}
}

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myteam.usersearch</groupId>
<artifactId>user-search</artifactId>
<version>1.0.0</version>
<name>user-search</name>
<description>user-search</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<packaging>jar</packaging>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.mycompany.myteam.usersearch.UserSearchApplication</start-class>
</properties>

<dependencies>

<!-- Commons -->
<dependency>
<groupId>com.mycompany.myteam.common</groupId>
<artifactId>team-common</artifactId>
<version>1.0.0</version>
</dependency>]

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

最佳答案

我将您的源文件和 XML 文件复制到新的 Maven 项目中。我在评论中所写的作为有根据的猜测实际上是正确的:

  • 您不需要任何 AspectJ LTW 或 CTW,即您可以删除

    • aop.xml(以及META-INF文件夹),
    • AspectJ Maven 插件,
    • 对 AspectJ 运行时和编织器的依赖,
    • Maven Surefire LTW 配置(删除整个插件)。
  • Spring AOP 可以很好地与您的多模块设置配合使用。

  • 正如我所说,请修复您的切入点以匹配子包:
@Around(
"execution(public * com.mycompany.myteam..*Controller.*(..)) && " +
"!execution(public * com.mycompany.myteam..AuditController.*(..))"
)

我必须修复您的类中的一些内容(例如,重命名方面以匹配记录器名称,添加虚拟服务和具有相应方法的 DTO 以便使代码编译)并将 H2 数据库驱动程序添加到您的 用户搜索 POM。然后一切都很顺利。

关于java - 如何使用加载时间编织在一个 jar 中创建 @Aspect 并拦截另一个 jar 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280280/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com