Spring AOP with AspectJ - Load time weaving doubts

孤街醉人 提交于 2019-12-13 04:04:52

问题


Reading the Spring AOP documentation (link), I'm having a hard time (maybe also because english is not my native language) understanding these paragraphs.

First, I read

Further, in certain environments, this support enables load-time weaving without making any modifications to the application server’s launch script that is needed to add -javaagent:path/to/aspectjweaver.jar or (as we describe later in this section) -javaagent:path/to/org.springframework.instrument-{version}.jar (previously named spring-agent.jar).

And

Developers modify one or more files that form the application context to enable load-time weaving

Which files? @Aspect classes and aop.xml files?


Then, when describing an example in the same sub-chapter, they say

We have one last thing to do. The introduction to this section did say that one could switch on LTW selectively on a per-ClassLoader basis with Spring, and this is true. However, for this example, we use a Java agent (supplied with Spring) to switch on the LTW. We use the following command to run the Main class shown earlier:

And they apply a Java Agent to the JVM.

-javaagent:C:/projects/foo/lib/global/spring-instrument.jar

Now I have a couple of doubts.

If I @EnableLoadTimeWeaving, do I need the spring-instrument Jar file as Java Agent?
I suppose the answer is yes, because we need to add bytecode to the class file before loading it. But a confirmation would be much appreciated.

The Jar naming is a little ambiguos, first they mention spring-agent.jar, then they use org.springframework.instrument-{version}.jar, and then spring-instrument.jar.
Are we always talking about the same Jar file?


回答1:


I see from another question you asked that you are using Spring Boot and running a fat jar. In this case you don't need @EnableLoadTimeWeaving or spring-instrument (formerly known as spring-agent). Just ignore them if you are not running in an appserver for which you don't control the agent path.

I opened an issue for you about the confusion in the docs: https://github.com/spring-projects/spring-framework/issues/22429.



来源:https://stackoverflow.com/questions/54746336/spring-aop-with-aspectj-load-time-weaving-doubts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!