Dominik
Dominik

Reputation: 103

Spring AspectJ load time weaving javaagent

I want to use AspectJ to add a filter whenever a Hibernate session is opened. I created an aspect and tested it for my own classes and it worked, however for the Hibernate session it does not do anything.

I created a META-INF/aop.xml in the resources:

<aspectj>

<weaver options="-Xreweavable -verbose -showWeaveInfo">
    <include within="immo.api.config.multitenancy.TestAspect"/>
    <include within="org.hibernate.internal.SessionFactoryImpl.SessionBuilderImpl"/>
</weaver>

<aspects>
    <aspect name="immo.api.config.multitenancy.TestAspect"/>
</aspects>

When I start my programm, i get the following exception:

... Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-instrument-{version}.jar

I followed those tutorials:

Tutorial 1 Tutorial 2

I saw that I have to pass JVM arguments as the exception also tells, so I tried to do the following in the build gradle:

apply plugin: 'application'
applicationDefaultJvmArgs = ["-javaagent:spring-instrument.jar", "-javaagent:aspectjweaver.jar"]

the applicationDefaultJvmrgs are never used according to IntelliJ and i still get the same exception.

So can anybody help me to pass the JvmArgs correctly in gradle? Or, even better, tell me how to get the job done using a custom LoadTimeWeaver?

Thanks in advance!

EDIT:

Maybe it was unclear. Problem is that the aop.xml is never used and I just need to know how to tell the jvm to use javaagent with gradle

Upvotes: 1

Views: 1369

Answers (1)

Dominik
Dominik

Reputation: 103

I found the solution to my

https://guypaddock.github.io/posts/aspectj-native-syntax-with-spring-ltw-and-gradle/

This approach fixes my problem.

Upvotes: 2

Related Questions