1

对 Spring AOP 使用 Pointcut 不起作用,否则效果很好。请帮我解决这个问题。

没有切入点工作代码:

@Before("execution(public * spring.beans.Triangle.set*(*))")    
public void beforeonlySetTriangle_FirstAspect()
{
    System.out.println("beforeonlySetTriangle_FirstAspect only for Triangle is called");
}
@Before("execution(public * spring.beans.Triangle.set*(*))")    
public void beforeonlySetTriangle_SecondAspect()
{
    System.out.println("beforeonlySetTriangle_SecondAspect only for Triangle is called");
}

使用切入点代码给出错误:

@Before("setTrianglePointCutter()") 
public void beforeonlySetTriangle_FirstAspect()
{
    System.out.println("beforeonlySetTriangle_FirstAspect only for Triangle is called");
}
@Before("setTrianglePointCutter()") 
public void beforeonlySetTriangle_SecondAspect()
{
    System.out.println("beforeonlySetTriangle_SecondAspect only for Triangle is called");
}

@Pointcut("execution(public * spring.beans.Triangle.set*(*))")
public void setTrianglePointCutter()
{

}

例外 :

Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut setTrianglePointCutter
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:193)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:182)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:163)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:209)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:263)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:295)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 11 more

spring.xml 配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

 <aop:aspectj-autoproxy />

<bean id="triangle" class="spring.beans.Triangle">
<property name="name" value="TriangleName"></property>
</bean>

<bean id="circle" class="spring.beans.Circle">
<property name="name" value="CircleName"></property>
</bean>

<bean id="shapeService" class="spring.service.ShapeService" autowire="byName"/>
<bean id="loggingAspect" class="spring.aspect.LoggingAspect" />


</beans>
4

2 回答 2

3

这篇文章看来,旧版本的运行时 AspectJ jar 会发生错误。升级到最新版本aspectjrt,即1.7.3

于 2013-10-20T15:52:56.947 回答
0

有时由于 aspectj jar 的版本冲突会导致这个问题,对我来说就是这种情况。我刚刚删除了 aspectjtools 依赖项,将 aspectjrt、aspectjweaver 更新到了最新版本,它解决了我的问题

于 2017-08-21T14:10:04.457 回答