如果spring不依赖于aspectj,为什么spring会抛出一个aspectj错误?

时间:2013-05-15 15:06:09

标签: spring aop aspectj spring-aop

我正在尝试设置Spring AoP框架,我不想依赖于AspectJ,所以我在bean xml配置文件中声明我的方面,建议等,类似于以下内容:

<bean id="systemAuthorizationsAspect" class="com.cp.pm.systemsettings.SystemAuthorizationsAspect" >
    <property name="sysAuthorizations" ref="authorizations" />
</bean>
<bean id="authorizations" class="com.hp.cp.pm.systemsettings.SystemAuthorizationsImpl">
    <property name="authSettingsRegistry" ref="systemSettingsRegistry" />
</bean>
<aop:config>
    <aop:aspect id="createPlanAspect" ref="systemAuthorizationsAspect">
         <aop:before pointcut="execution(* com.hp.cp.web.api.plan.PlanApi.createPlan(..))" method="authorizePlanCreation"/>
    </aop:aspect>
</aop:config>

每当我指定切入点时,我都会收到以下错误:

BeanPostProcessor before instantiation of bean failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': 
Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; 
nested exception is org.springframework.beans.
factory.BeanCreationException: 
Error creating bean with name '(inner bean)': 
Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJExpressionPointcut] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name '(inner bean)': 
Instantiation of bean failed; nested exception is **java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException**

当我包含aspectjweaver.jar时,我可以完成这项工作。但事实并非如此。有什么想法吗?

提前致谢

2 个答案:

答案 0 :(得分:5)

请参阅Spring文档Enabling @AspectJ Support

  

可以使用XML或Java样式配置启用@AspectJ支持。在任何一种情况下,您还需要确保AspectJ的aspectjweaver.jar库位于应用程序的类路径中(版本1.6.8或更高版本)。

如果你想使用aspectj提供的功能,你当然需要一些aspectj库。但是我想在运行时将它放在类路径上就足够了,所以你的项目不一定会对这个jar有一个编译时依赖。

答案 1 :(得分:1)

为了让它在不依赖于AspectJ的情况下工作,切入点不能使用ApsectJ表达式语言。

例如,从zagyi提供的链接,以下代码具有使用AspectJ表达式语言声明的切入点:

<aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
      expression="execution(* com.xyz.myapp.service.*.*(..))"/>
</aop:aspect>

下一段代码是普通的老香草Spring AoP:

<aop:config>
<aop:pointcut id="businessService"
    expression="com.xyz.myapp.SystemArchitecture.businessService()"/>
</aop:config>

我的新问题是找到有关预期语法的任何文档。应该有退货类型吗?参数?等?