Spring为自定义Aspect抛出ClassNotFound

时间:2016-08-09 09:23:26

标签: java spring spring-aop

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

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBox x:Name="txtName" Grid.Row="0">
        <TextBox.Text>
            <Binding Path="Name" NotifyOnValidationError="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <common:RequiredFieldValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBlock Grid.Row="1" Text="{Binding ElementName=txtName,Path=(Validation.Errors)[0].ErrorContent}"
               Visibility="{Binding ElementName=txtName,Path=Validation.HasError,Converter=...}" />
</Grid>

以下是分析建议:

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

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

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

和POM:

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

对于这篇长篇文章感到抱歉,但我阅读并重新阅读了几本指南,而且我目前对此感到难过。

1 个答案:

答案 0 :(得分:1)

找到它。

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

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

我稍后使用class对其进行了定义,但无论如何,这样做是因为它使文件.aj而不是.java,因此javac没有编译它所以.class文件在运行时丢失了。

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