2
votes

I'm attempting to setup the Spring AoP framework and I don't want to depend on AspectJ, so I'm declaring my aspects, advice etc in a bean xml configuration file similar to the following:

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

I recieve the following error whenever I specify a pointcut as seen above:

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

I can make this work when I include the aspectjweaver.jar. Yet this should not have to be the case. Any thoughts?

Thanks in advance

2

2 Answers

5
votes

See Spring docs Enabling @AspectJ Support:

The @AspectJ support can be enabled with XML or Java style configuration. In either case you will also need to ensure that AspectJ's aspectjweaver.jar library is on the classpath of your application (version 1.6.8 or later).

Of course you will need some aspectj library if you want to use features provided by aspectj. However I guess it's enough to have it on the classpath at runtime, so your project won't necessarily have a compile time dependency on this jar.

2
votes

To get this to work without depending on AspectJ, the pointcut cannot use the ApsectJ expression language.

For example from the link provided by zagyi, the following code has the pointcut declared with AspectJ expression language:

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

And the next block of code is plain old vanilla Spring AoP:

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

My new problem is finding any documentation on the expected syntax. Should there be a return type? Parameters? Etc?