0

I have followed the steps given in the following spring docs: https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw

My project is a monolith with modules as such :

ApplicationService in module m1. Child module m2 with parent m1.(m1 has a dependency on m2)

aop.xml file in m1/WebContent/META-INF/aop.xml as follows :

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>    
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="m2.*"/>
    </weaver>

<aspects>
    <!-- weave in just this aspect -->
    <aspect name="m2.security.FieldPermissionAspect"/>
</aspects>

Application-context.xml file in m1/src/main/webapp/WEB-INF as follows :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
...

<mvc:annotation-driven />
<aop:aspectj-autoproxy />
<task:annotation-driven />
<!-- this switches on the load-time weaving -->
<context:load-time-weaver weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver"/>

My aspect in m2.security is as follows:

package m2.security;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class FieldPermissionAspect {


    @Pointcut("execution(public * *(..))")
    public void combinedPointcut() {}

    @Around("combinedPointcut()")
    public void aroundMapper(ProceedingJoinPoint joinPoint) {
        ...
    }

    @Around("cflow(combinedPointcut())")
    public void aroundSetter(ProceedingJoinPoint joinPoint) {
        ...
    }
}

AspectJ dependencies in pom.xml in m2:

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>

</dependencies>

When I run it in tomcat environment, I get the following error :

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'cflow(combinedPointcut())' contains unsupported pointcut primitive 'cflow'
at org.aspectj.weaver.tools.PointcutParser.validateAgainstSupportedPrimitives(PointcutParser.java:425)
at org.aspectj.weaver.tools.PointcutParser.resolvePointcutExpression(PointcutParser.java:311)
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:294)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:193)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:170)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:330)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1577)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 66 more

I think it's because spring is still using Spring AOP instead of AspectJ in my aspect. What am I missing here?

  • Per documentation , Spring AOP would throw IllegalArgumentException when cflow is used in pointcut expression . Please check this answer https://stackoverflow.com/a/6523083/4214241 for the details on UnsupportedPointcutPrimitiveException – R.G Jan 03 '20 at 11:41
  • @R.G I'm aware of that. That's why I'm trying to configure aspectj LTW instead of Spring AOP. But I'm still getting this error, so my question is why is AspectJ LTW not working with these configuration. – Lakshya Yadav Jan 03 '20 at 13:28
  • Try removing from your Application-context.xml. alone should enable load time weaving – R.G Jan 03 '20 at 13:42
  • I tried that. Didn't work. Also tried removing classLoader weaver class tag. That also didn't work. – Lakshya Yadav Jan 03 '20 at 15:26

1 Answers1

3

If you want to use AspectJ LTW instead of Spring AOP, you should not use Spring AOP configuration. so please get rid of <aop:aspectj-autoproxy />. Despite the name it is about Spring AOP, not AspectJ. AspectJ does not use any proxies.

As for your error message, ...

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:
  Pointcut expression 'cflow(combinedPointcut())'
  contains unsupported pointcut primitive 'cflow'

... it occurs because you are still using Spring AOP, AspectJ LTW is not used. So you are having a configuration issue. Does it work if you start your container with

-javaagent:/path/to/aspectjweaver.jar

on the Java command line?

Last, but not least, like I said 3x already in your previous question, please provide an MCVE on GitHub, then I can analyse your problem. I really cannot do more than speculate with the bits of information provided by you here. So please do what I asked you to and help me to help you. Thanks.

kriegaex
  • 63,017
  • 15
  • 111
  • 202