Spring vs Aspects in 3rd Party JARs











up vote
0
down vote

favorite
1












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!!










share|improve this question
























  • 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















up vote
0
down vote

favorite
1












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!!










share|improve this question
























  • 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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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!!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues