Spring vs Aspects in 3rd Party JARs
up vote
0
down vote
favorite
Fellow members, it looks like there are some gray areas in my understanding about how aspects work with Spring. Need your help on understanding few things.
I was working on an assignment recently where I wrote a custom logging/audit framework. The logging/auditing part is required to be executed before and after business methods are called, so Aspects was a natural choice to achieve this. I created a separate Java project, implemented all the logging/audit logic, packaged the project as a JAR (let's call it ABC.jar), and this JAR is used by another Java/Spring project (let's call this project as PQR)
ABC.jar has an annotation @AuditLoggable, and methods in PQR should be annotated with this annotation so that logging/audit logic is executed.
In Eclipse, the logging/audit logic is executed properly before and after @AuditLoggable annotated methods as soon as I add ABC.jar as a dependency in pom.xml.
But, when I packaged PQR as a JAR using maven and ran my application, I saw that the logging/audit logic was not executed at all for the methods who had the annotation above them.
I looked for some help on internet and SO, and I found few resources which indicated that I need to make use of "aspectj-maven-plugin" which weaves the aspects into source code at compile time. I updated the pom.xml and added the required plugin, post which my code is working and aspects are getting called as expected when PQR is packaged as JAR.
Few Questions -
1) Why there is a difference in behavior between when project PQR is run in Eclipse (Spring boot main class --> Run as Java Application) vs when program is run through command line (mvn clean package && java -jar {path-to-PQR.jar}?
2) Since my aspects were present in 3rd party JAR (ABC.jar), I was required to make use of Maven plugin to weave them with my project. But then, how come the annotations that come with Spring and Hibernate libraries work out of the box? Why I am not required this 'weaving' step to use annotations from Spring/Hibernate libraries? Even these libraries are 3rd party libraries from my project's perspective, right?
I know the concept of how aspects work with Spring and how Spring makes use of dynamic proxies or cglib, or what is load time vs compile time vs runtime weaving, but I still need to understand how all of these pieces fit together with the project setup that I described above.
Thanks a lot guys!!
spring-boot aspectj load-time-weaving aspectj-maven-plugin compile-time-weaving
add a comment |
up vote
0
down vote
favorite
Fellow members, it looks like there are some gray areas in my understanding about how aspects work with Spring. Need your help on understanding few things.
I was working on an assignment recently where I wrote a custom logging/audit framework. The logging/auditing part is required to be executed before and after business methods are called, so Aspects was a natural choice to achieve this. I created a separate Java project, implemented all the logging/audit logic, packaged the project as a JAR (let's call it ABC.jar), and this JAR is used by another Java/Spring project (let's call this project as PQR)
ABC.jar has an annotation @AuditLoggable, and methods in PQR should be annotated with this annotation so that logging/audit logic is executed.
In Eclipse, the logging/audit logic is executed properly before and after @AuditLoggable annotated methods as soon as I add ABC.jar as a dependency in pom.xml.
But, when I packaged PQR as a JAR using maven and ran my application, I saw that the logging/audit logic was not executed at all for the methods who had the annotation above them.
I looked for some help on internet and SO, and I found few resources which indicated that I need to make use of "aspectj-maven-plugin" which weaves the aspects into source code at compile time. I updated the pom.xml and added the required plugin, post which my code is working and aspects are getting called as expected when PQR is packaged as JAR.
Few Questions -
1) Why there is a difference in behavior between when project PQR is run in Eclipse (Spring boot main class --> Run as Java Application) vs when program is run through command line (mvn clean package && java -jar {path-to-PQR.jar}?
2) Since my aspects were present in 3rd party JAR (ABC.jar), I was required to make use of Maven plugin to weave them with my project. But then, how come the annotations that come with Spring and Hibernate libraries work out of the box? Why I am not required this 'weaving' step to use annotations from Spring/Hibernate libraries? Even these libraries are 3rd party libraries from my project's perspective, right?
I know the concept of how aspects work with Spring and how Spring makes use of dynamic proxies or cglib, or what is load time vs compile time vs runtime weaving, but I still need to understand how all of these pieces fit together with the project setup that I described above.
Thanks a lot guys!!
spring-boot aspectj load-time-weaving aspectj-maven-plugin compile-time-weaving
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Fellow members, it looks like there are some gray areas in my understanding about how aspects work with Spring. Need your help on understanding few things.
I was working on an assignment recently where I wrote a custom logging/audit framework. The logging/auditing part is required to be executed before and after business methods are called, so Aspects was a natural choice to achieve this. I created a separate Java project, implemented all the logging/audit logic, packaged the project as a JAR (let's call it ABC.jar), and this JAR is used by another Java/Spring project (let's call this project as PQR)
ABC.jar has an annotation @AuditLoggable, and methods in PQR should be annotated with this annotation so that logging/audit logic is executed.
In Eclipse, the logging/audit logic is executed properly before and after @AuditLoggable annotated methods as soon as I add ABC.jar as a dependency in pom.xml.
But, when I packaged PQR as a JAR using maven and ran my application, I saw that the logging/audit logic was not executed at all for the methods who had the annotation above them.
I looked for some help on internet and SO, and I found few resources which indicated that I need to make use of "aspectj-maven-plugin" which weaves the aspects into source code at compile time. I updated the pom.xml and added the required plugin, post which my code is working and aspects are getting called as expected when PQR is packaged as JAR.
Few Questions -
1) Why there is a difference in behavior between when project PQR is run in Eclipse (Spring boot main class --> Run as Java Application) vs when program is run through command line (mvn clean package && java -jar {path-to-PQR.jar}?
2) Since my aspects were present in 3rd party JAR (ABC.jar), I was required to make use of Maven plugin to weave them with my project. But then, how come the annotations that come with Spring and Hibernate libraries work out of the box? Why I am not required this 'weaving' step to use annotations from Spring/Hibernate libraries? Even these libraries are 3rd party libraries from my project's perspective, right?
I know the concept of how aspects work with Spring and how Spring makes use of dynamic proxies or cglib, or what is load time vs compile time vs runtime weaving, but I still need to understand how all of these pieces fit together with the project setup that I described above.
Thanks a lot guys!!
spring-boot aspectj load-time-weaving aspectj-maven-plugin compile-time-weaving
Fellow members, it looks like there are some gray areas in my understanding about how aspects work with Spring. Need your help on understanding few things.
I was working on an assignment recently where I wrote a custom logging/audit framework. The logging/auditing part is required to be executed before and after business methods are called, so Aspects was a natural choice to achieve this. I created a separate Java project, implemented all the logging/audit logic, packaged the project as a JAR (let's call it ABC.jar), and this JAR is used by another Java/Spring project (let's call this project as PQR)
ABC.jar has an annotation @AuditLoggable, and methods in PQR should be annotated with this annotation so that logging/audit logic is executed.
In Eclipse, the logging/audit logic is executed properly before and after @AuditLoggable annotated methods as soon as I add ABC.jar as a dependency in pom.xml.
But, when I packaged PQR as a JAR using maven and ran my application, I saw that the logging/audit logic was not executed at all for the methods who had the annotation above them.
I looked for some help on internet and SO, and I found few resources which indicated that I need to make use of "aspectj-maven-plugin" which weaves the aspects into source code at compile time. I updated the pom.xml and added the required plugin, post which my code is working and aspects are getting called as expected when PQR is packaged as JAR.
Few Questions -
1) Why there is a difference in behavior between when project PQR is run in Eclipse (Spring boot main class --> Run as Java Application) vs when program is run through command line (mvn clean package && java -jar {path-to-PQR.jar}?
2) Since my aspects were present in 3rd party JAR (ABC.jar), I was required to make use of Maven plugin to weave them with my project. But then, how come the annotations that come with Spring and Hibernate libraries work out of the box? Why I am not required this 'weaving' step to use annotations from Spring/Hibernate libraries? Even these libraries are 3rd party libraries from my project's perspective, right?
I know the concept of how aspects work with Spring and how Spring makes use of dynamic proxies or cglib, or what is load time vs compile time vs runtime weaving, but I still need to understand how all of these pieces fit together with the project setup that I described above.
Thanks a lot guys!!
spring-boot aspectj load-time-weaving aspectj-maven-plugin compile-time-weaving
spring-boot aspectj load-time-weaving aspectj-maven-plugin compile-time-weaving
edited Nov 11 at 0:22
asked Nov 11 at 0:11
Neeraj
12
12
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago
add a comment |
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244672%2fspring-vs-aspects-in-3rd-party-jars%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It would have been better to share some code, ideally an MCVE. Then maybe I would have seen what kind of AOP framework you were using before switching to compile-time weaving. Was it proxy-based Spring AOP or full AspectJ via load-time weaving configuration? This makes a big difference. After knowing that, we can follow up on why it did not work. I see no reason why either of the two approaches should not work if configured correctly.
– kriegaex
2 days ago