0

I have a requirement to intercept certain method for apache POI API(HSSFCell setCellValue), and I planed to use Spring AspectJ to fulfill this requirement. But after some testing I found it's not working. Not sure it that possible to weave the external jar like apache POI?

here is the relevant code from my pom.xml:

     <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi</artifactId>
         <version>3.17</version>
     </dependency>
     <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.13</version>
    </dependency>

<build>    
     <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.11</version>
                    <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                        <weaveDependencies>
                            <weaveDependency>
                                <groupId>org.apache.poi</groupId>
                                <artifactId>poi</artifactId>
                            </weaveDependency>               
                        </weaveDependencies>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>                      
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
</build>

and my aspectJ java class:

@Aspect
@Component
public class TestInterceptor
{
    @Pointcut("execution(* org.apache.poi..*.*(..))")
    public void methodPointcut()
    {
    }

    @Before("methodPointcut()")
    public void Interceptor(JoinPoint joinPoint)
    {
         System.out.println("hello world");
    }

}

By right when I have any interaction with apache POI API (e.g. call the setCellValue method on HSSFCell), it will print hello world to the console, but nothing happened in my case.

Koenigsegg
  • 575
  • 1
  • 9
  • 23
  • It is possible. Either you use post-compile binary weaving and create a woven version of the Apache library, package it into a new JAR and deploy it instead of the original, maybe with another Maven group ID and uploaded to your artifact repository first, then you don't need to repeat this step every time you compile, only when you change the library's version number. – kriegaex Apr 30 '19 at 04:33
  • Or you use load-time weaving instead, weaving the Apache library on the fly while it is loaded into your application. But then you need to control the start-up process of your JVM or application server because you need to provide the weaver as a Java agent on the command line. – kriegaex Apr 30 '19 at 04:35
  • Third option: Instead of `execution()` inside the library you could intercept `call()` inside your own classes if that is where all the relevant the calls come from. Then you don't need to weave anything into the library, neither via post-compile nor via load-time weaving. You would just weave your own code. – kriegaex Apr 30 '19 at 04:37
  • Which of the three ways shall it be? I can help with each of them. We can talk more and after you provide an [MCVE](http://stackoverflow.com/help/mcve), not just snippets of aspect code and POM. Besides, we are really talking about AspectJ here, not about proxy-based Spring AOP! The latter will not help you with non-Spring classes. – kriegaex Apr 30 '19 at 04:39

0 Answers0