Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AspectJ based load time weaving using an LTW aware classloader without Java agent on "java -jar app.jar" #739

Open
alex-konkov opened this issue Apr 27, 2014 · 34 comments
Labels
type: enhancement A general enhancement

Comments

@alex-konkov
Copy link

AspectJ based load time weaving (LTW) can be set up with Spring Boot, however when running as a dead simple "java -jar app.jar" the classes are not woven until Java agent is specified. (AspectJ + LTW + self contained JAR) is a potentially cool combination for many Spring Boot powered projects that need to leverage AOP without needing to play any additional tricks (or worrying about not forgetting to).

This feature request is about adding an AspectJ LTW bootstrapping code straight into the Spring Boot launcher where a specific class-loader is initialised. Then, the client projects that would like to stick with the simplest "java -jar app.jar" startup way would be able to leverage power of AspectJ LTW weaving feature out of the box.

http://stackoverflow.com/questions/23219772/spring-boot-and-aspectj-based-ltw-without-java-agent/23248830?noredirect=1#comment35709788_23248830

@alex-konkov alex-konkov changed the title AspectJ based load time weaving using an LTW aware classloader without Java agent AspectJ based load time weaving using an LTW aware classloader without Java agent on "java -jar app.jar" Apr 27, 2014
@alex-konkov
Copy link
Author

Would be interesting what do people think on that and if there is a chance to get it done in spring-boot some day. Any thoughts? thx

@MuzuMatt
Copy link

I would certainly welcome this as an enhancement. This is the one limitation that prevents some of my apps from being contained in a single jar file.

@snicoll snicoll modified the milestones: 1.1.0.M2, 1.1.0.RC1 May 25, 2014
@mahe-work
Copy link

This would be very much appreciated. So far we've been stuck with Spring dynamic proxy based AOP and its limitations which has lead to all kinds of ugliness such as static support classes duplicating the aspect's behaviour etc.

The reason for this has been that we have been running our applications with Maven Jetty plugin and don't want to ship the aspectj agent jar separately with the application. Ideally it should come from repository just like any other dependency without the need to specify it's exact location on command line.

We're at the moment changing over to spring boot, gradle and other cool stuff and this would be a huge plus.

@philwebb philwebb modified the milestones: 1.2.0.M0, 1.2.0.M1 Sep 2, 2014
@kmandeville
Copy link

Any progress on this? Looks like it keeps slipping milestones...

@benneq
Copy link

benneq commented Oct 9, 2014

Would be cool if this makes it in 1.2.0.

@wilkinsona
Copy link
Member

I have some doubts about the usability of this feature. Perhaps those who are interested in it can allay my fears. My primary concern is that launching your app via java -jar would give you load-time weaving automatically, whereas launching it via the main method in your IDE would not. A similar problem would exist when running integration tests (there's an issue open against Spring's Test framework for this).

While having to configure the agent is clunky, I like the fact that it's consistent.

@philwebb philwebb removed this from the 1.2.0.M2 milestone Oct 9, 2014
@alex-konkov
Copy link
Author

wilkinsona, your point is valid but it is rather consistently not implemented in either usage case. The change towards LTW support appears to be evolutionary as long as spring boot brings the idea of self-sufficient package.

@kjozsa
Copy link

kjozsa commented May 5, 2015

Also, a quick comment above as "being consistent" - if you have a spring-boot project with Eclipselink which does require LTW, running it with "mvn spring-boot:run" will work and running the packaged jar file with "java -jar" will just fail.

An example of this problem is demonstrated with this project: https://github.com/dsyer/spring-boot-sample-data-eclipselink - clone it, mvn package and java -jar the built jar, it fails badly.

@wilkinsona
Copy link
Member

It only works with mvn spring-boot:run because the agent is configured in the pom.xml:

<configuration>
    <agent>${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</agent>
</configuration>

If you don't supply the equivalent configuration when running the application using java -jar it will, quite reasonably, fail.

@imod
Copy link

imod commented Aug 18, 2015

is there anything we can do to make this happen?

@wilkinsona
Copy link
Member

I've yet to see a compelling argument against my usability concerns described above.

@imod
Copy link

imod commented Aug 18, 2015

hmm, not sure the miss of a feature on the test framework is a compelling argument...

For us, this missing feature means we can't use load time weaving on cloudfoundry with any spring boot application. Because the deploy unit is a single JAR - if we wanna change this, then we have to fork the java_buildpack and add this functionality (that's nothing I really wanna do).
Looking at the importance of spring for cloudfoundry (in both ways) I would assume this argument is stronger then the one about the test framework.

In fact implementing it here will maybe have @sbrannen to take an other look at the mention issue in the test framework too.

@wilkinsona
Copy link
Member

hmm, not sure the miss of a feature on the test framework is a compelling argument...

As I said above, that's a secondary concern. My primary concern is about the complexity of having things behave differently when you run using java -jar, vs launching the main method in your IDE, vs launching via our Maven or Gradle plugins. Rather than having to document different behaviour depending on how you launch your application, I like the consistency of the current behaviour. As things stand, you consistently have to configure the agent. Adding some magic to java -jar would break that consistency.

The Java buildpack already has support for a number of Java agents. Another way to tackle your particular problem would be via an enhancement to the buildpack. /cc @cgfrost

@lukiano
Copy link

lukiano commented Aug 18, 2015

This is what I did to make it work:

pom.xml

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <layout>ZIP</layout>
    <mainClass>org.springframework.boot.SpringApplication</mainClass>
  </configuration>
</plugin>

ZIP layout so that it uses the org.springframework.boot.loader.PropertiesLauncher

application.properties

loader.classLoader=packageName.TransformingClassLoader
loader.main=org.springframework.boot.SpringApplication
spring.main.sources=packageName.Config

Here I tell the PropertiesLauncher to use my own classLoader, and that the main program is the standard SpringApplication, with packageName.Config as my Spring configuration file.

TransformingClassLoader.java
https://gist.github.com/lukiano/114cb01a59ddd9880aa7
based on org.springframework.instrument.classloading.ShadowingClassLoader and org.springframework.core.OverridingClassLoader

And then my Config file implements LoadTimeWeavingConfigurer

public LoadTimeWeaver getLoadTimeWeaver() {
  return new ReflectiveLoadTimeWeaver(Thread.currentThread().getContextClassLoader());
}

So basically it loads the entire Spring context under my own ClassLoader which implements the transformer methods that ReflectiveLoadTimeWeaver expects. And it still works when it creates a fat-jar, so it doesn't depend on being run with mvn spring-boot:run

@imod
Copy link

imod commented Aug 18, 2015

@lukiano nice, that looks like a solution which would also solve the concerns @wilkinsona has, with some further work this might be doable even without the ZIP packaging and therefore integrate it into boot directly.
I just found an other solution which might be interesting too: https://github.com/subes/invesdwin-instrument

@lukiano
Copy link

lukiano commented Aug 18, 2015

I see, that one uses tools.jar to inject the agent itself by retrieving the jvm's process id. A solution I considered before, but discarded to avoid a dependency on the JDK

@philwebb philwebb added the for: team-attention An issue we'd like other members of the team to review label Nov 11, 2015
@wilkinsona wilkinsona added team-meeting and removed for: team-attention An issue we'd like other members of the team to review labels Nov 25, 2015
@philwebb
Copy link
Member

philwebb commented Mar 14, 2018

No more +1 comments please (I've removed the existing ones). They just make the issue harder to read. Just click the "thumbs up" reaction on the top comment if you want to vote.

@mauromol
Copy link

As I said above, that's a secondary concern. My primary concern is about the complexity of having things behave differently when you run using java -jar, vs launching the main method in your IDE, vs launching via our Maven or Gradle plugins.

But what about applying also the following:

  • enhancing the Gradle org.springframework.boot Plugin to add the necessary references to the agent
  • decorating the Spring Boot App Run Configuration in Eclipse to add the necessary references to the agent
    In this way, the behaviour would be aligned between "java -jar", Gradle and Eclipse launching of the application.
    Perhaps something similar may be achieved with Maven and other IDEs...

@ade90036
Copy link

+1

@kriegaex
Copy link

loader.classLoader=packageName.TransformingClassLoader
loader.main=org.springframework.boot.SpringApplication
spring.main.sources=packageName.Config

Here I tell the PropertiesLauncher to use my own classLoader, and that the main program is the standard SpringApplication, with packageName.Config as my Spring configuration file.

@lukiano, this whole approach is not working for me. I started with my existing playground project based on Boot 2.1.8. But even after upgrading to 2.5.6, it is not working. Could we please have a chat on Gitter and sort this out together? I am an experienced AspectJ user (and AspectJ co-maintainer), but unexperienced in Spring Core and Spring Boot. I really would like to know if there is any way to get simple Spring Boot applications working from IDE or via java -jar after packaging without having to use a Java agent on the command line. It is just an FAQ when people consult me, and all I can answer is that the Spring documentation says that you can configure Spring to run without -javaagent:, but in reality it simply does not work in Spring Boot, which is what most developers use nowadays when starting to experiment with AOP, both Spring AOP and AspectJ LTW.

@lukiano
Copy link

lukiano commented Oct 29, 2021

I'd love to help truly but I haven't worked on this project (or any Spring project really) for the last 5.5 years. My memories on how Spring and in particular AspectJ worked are long gone.

@vangogh-ken
Copy link

Now have to use javaagent instead

@ade90036
Copy link

ade90036 commented Feb 23, 2022 via email

@kriegaex
Copy link

@ade90036, your comment is a bit cryptic. What was painful and how did you achieve what you wanted? Certainly, adding --javaagent is not difficult. It is just not super convenient and sometimes not an option, depending on who rules the runtime environment, if that is what you mean.

I cannot relate to your comments about Spring Native, because that is a whole different world with GraalVM and the rest of its infrastructure. It is not helpful to discuss it here, because I think it is off-topic. Let us try not to compare apples and oranges. Starting a Spring Boot application from an IDE or from an uber JAR is quite different from setting up the infrastructure for Spring Native and wait for a lengthy compilation process before being able to use the then runtime-optimised application. One does not replace the other, the two concepts rather complement each other for different scenarios.

@ade90036
Copy link

ade90036 commented Feb 23, 2022 via email

@kriegaex
Copy link

kriegaex commented Mar 19, 2024

Hi all!

If you are running your applications as executable JARs on JRE 9+ (Spring Boot ones or other types), probably Agent Embedder Maven Plugin is what you want.

It enables you to embed a java agent in your executable JAR and have it started automatically using the Launcher-Agent-Class JVM mechanism.

Unique features added by this plugin and unavailable via the original JVM mechanism: You can

  • embed and run multiple agents (the JVM supports only one out of the box),
  • pass an option string to each agent, just like from the JVM command line.

Spoiler: I am the author of the plugin and also happen to be the current maintainer of AspectJ.

Edit: I forgot to mention, that in the case of the AspectJ weaver, as the JVM starts the agent very early, weaving will be active without extra Spring configuration and it should work for all classloaders - no more ClassLoader [...] does NOT provide an 'addTransformer(ClassFileTransformer)' method errors as seen when hot-attaching the weaver via spring-instrument.jar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests