【问题标题】:Why does spring throw an aspectj error if it does not depend on aspectj?如果spring不依赖aspectj,为什么会抛出aspectj错误?
【发布时间】:2013-05-15 15:06:09
【问题描述】:

我正在尝试设置 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 时,我可以完成这项工作。然而,这不应该是这样的。有什么想法吗?

提前致谢

【问题讨论】:

    标签: spring aop aspectj spring-aop


    【解决方案1】:

    请参阅 Spring 文档Enabling @AspectJ Support

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

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

    【讨论】:

    • 他明确表示,他既不想使用 AspectJ 也不想使用它的功能
    【解决方案2】:

    要使其在不依赖 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>
    

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

    【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多