gpt4 book ai didi

java - Spring 为自定义 Aspect 抛出 ClassNotFound

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

我正在尝试使用 Spring AOP,但遇到了麻烦。这是:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.codarama.diet.event.aop.ProfilingAdvice] for bean with name 'profilingAdvice' defined in class path resource [META-INF/test-contexts/testProfilingAdvice.xml]; nested exception is java.lang.ClassNotFoundException: org.codarama.diet.event.aop.ProfilingAdvice

以下是分析建议:

package org.codarama.diet.event.aop;

import com.google.common.base.Stopwatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.codarama.diet.component.ListenableComponent;
import org.codarama.diet.event.model.MinimizationEndEvent;
import org.codarama.diet.event.model.MinimizationEvent;
import org.codarama.diet.event.model.MinimizationStartEvent;

import java.util.Set;

@Aspect
public class ProfilingAdvice extends ListenableComponent {

@Around("org.codarama.diet.event.aop.IndexingAspect.get()")
public Object profileGet(ProceedingJoinPoint pjp) throws Throwable {
// some code
}

@Around("org.codarama.diet.event.aop.IndexingAspect.index()")
public Object profileIndex(ProceedingJoinPoint pjp) throws Throwable {
// some more code
}

@Around("org.codarama.diet.event.aop.MinimizationAspect.minimize()")
public Object profileMinimize(ProceedingJoinPoint pjp) throws Throwable {
// and some more code
}
}

我正在使用 XML 配置,以下是相关部分(我希望):

<context:spring-configured/>
<context:annotation-config/>
<aop:aspectj-autoproxy/>

...

<bean id="indexingAspect" class="org.codarama.diet.event.aop.IndexingAspect"/>
<bean id="minimizationAspect" class="org.codarama.diet.event.aop.MinimizationAspect"/>
<bean id="profilingAdvice" class="org.codarama.diet.event.aop.ProfilingAdvice" parent="listenableComponent"/>

...

<bean id="listenableComponent" class="org.codarama.diet.component.ListenableComponent" abstract="true">
<property name="eventBus" ref="statusUpdateEventBus"/>
</bean>

<bean id="statusUpdateEventBus" class="com.google.common.eventbus.AsyncEventBus">
<constructor-arg ref="eventBusExecutor"/>
</bean>

<bean id="eventBusExecutor" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
<constructor-arg value="1"/>
</bean>

和 POM:

    <org.springframework.version>
4.3.1.RELEASE
</org.springframework.version>
<aspectj.version>1.8.9</aspectj.version>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>

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

<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>

很抱歉这篇文章很长,但我读了又读了几本指南,目前有点难住了。

最佳答案

找到了。

事实证明,我之前已经使用 aspect AspectJ 关键字定义了切面。像这样:

@Aspect
public aspect ProfilingAdvice extends ListenableComponent {
// code
}

我后来用 class 定义了它,但无论如何,它所做的是它创建了文件 .aj 而不是 .java ,因此 javac 没有编译它,因此 .class 文件在运行时丢失。

我所要做的就是将 .aj 重命名为 .java,一切都很好。

关于java - Spring 为自定义 Aspect 抛出 ClassNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38846826/

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