Pointcut or Aspect Around All Service Methods with Annotation @Transactional(readOnly = false)












1














Is it possible to use Spring AOP or AspectJ to intercept all Service methods (contained in classes in the com.app.service.* package) having the annotation



@Transactional(readOnly = false)


(other elements possible as well in Spring's @Transactional annotation, but we only care about readOnly = false).



I could only find examples pertaining to pointcuts with simple Annotations, or @Annotation(value).



My preference would be to use straight Spring, if possible.



It would probably be something like the below, but not sure about the syntax.



@Around("execution(* com.app.service..*.*(..))" && @Transactional[??])










share|improve this question






















  • That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
    – klvenky
    Nov 19 '18 at 21:29
















1














Is it possible to use Spring AOP or AspectJ to intercept all Service methods (contained in classes in the com.app.service.* package) having the annotation



@Transactional(readOnly = false)


(other elements possible as well in Spring's @Transactional annotation, but we only care about readOnly = false).



I could only find examples pertaining to pointcuts with simple Annotations, or @Annotation(value).



My preference would be to use straight Spring, if possible.



It would probably be something like the below, but not sure about the syntax.



@Around("execution(* com.app.service..*.*(..))" && @Transactional[??])










share|improve this question






















  • That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
    – klvenky
    Nov 19 '18 at 21:29














1












1








1


1





Is it possible to use Spring AOP or AspectJ to intercept all Service methods (contained in classes in the com.app.service.* package) having the annotation



@Transactional(readOnly = false)


(other elements possible as well in Spring's @Transactional annotation, but we only care about readOnly = false).



I could only find examples pertaining to pointcuts with simple Annotations, or @Annotation(value).



My preference would be to use straight Spring, if possible.



It would probably be something like the below, but not sure about the syntax.



@Around("execution(* com.app.service..*.*(..))" && @Transactional[??])










share|improve this question













Is it possible to use Spring AOP or AspectJ to intercept all Service methods (contained in classes in the com.app.service.* package) having the annotation



@Transactional(readOnly = false)


(other elements possible as well in Spring's @Transactional annotation, but we only care about readOnly = false).



I could only find examples pertaining to pointcuts with simple Annotations, or @Annotation(value).



My preference would be to use straight Spring, if possible.



It would probably be something like the below, but not sure about the syntax.



@Around("execution(* com.app.service..*.*(..))" && @Transactional[??])







java spring aop aspectj pointcut






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 19:28









gene b.gene b.

1,67352554




1,67352554












  • That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
    – klvenky
    Nov 19 '18 at 21:29


















  • That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
    – klvenky
    Nov 19 '18 at 21:29
















That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
– klvenky
Nov 19 '18 at 21:29




That should be possible. I've worked sometime back on spring and I remember using a xml configuration to achieve the same. We used to mark all transactions as read only and explicitly configure required ones to be able write.
– klvenky
Nov 19 '18 at 21:29












2 Answers
2






active

oldest

votes


















0














Unfortunately no easy way to do that. Even when we have an Annotation-based pointcut, e.g.



@Aspect
@Component
@EnableAspectJAutoProxy
public class WriteTransactionAspectBean {

@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void test(org.springframework.transaction.annotation.Transactional t) {
System.out.println("TEST");
}

}


the issue is the Annotations aren't our own, they come from an external JAR (Hibernate). This would require Load-Time Weaving or some other difficult workaround.



Aspectj: intercept method from external jar



But to make things even worse, Annotations need RetentionPolicy=RUNTIME in order to be "discovered" by Pointcuts. And we would need to go thru every method and add this specification to every @Transactional. There's no way to automatically make all @Transactional's Runtime-retainable in the application.






share|improve this answer





















  • This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
    – kriegaex
    Nov 25 '18 at 8:27





















0














You want to use a pointcut like this:



execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))





share|improve this answer





















  • The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
    – gene b.
    Nov 26 '18 at 15:12










  • I do not understand what you just wrote.
    – kriegaex
    Nov 29 '18 at 5:25











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',
autoActivateHeartbeat: false,
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%2f53381382%2fpointcut-or-aspect-around-all-service-methods-with-annotation-transactionalrea%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Unfortunately no easy way to do that. Even when we have an Annotation-based pointcut, e.g.



@Aspect
@Component
@EnableAspectJAutoProxy
public class WriteTransactionAspectBean {

@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void test(org.springframework.transaction.annotation.Transactional t) {
System.out.println("TEST");
}

}


the issue is the Annotations aren't our own, they come from an external JAR (Hibernate). This would require Load-Time Weaving or some other difficult workaround.



Aspectj: intercept method from external jar



But to make things even worse, Annotations need RetentionPolicy=RUNTIME in order to be "discovered" by Pointcuts. And we would need to go thru every method and add this specification to every @Transactional. There's no way to automatically make all @Transactional's Runtime-retainable in the application.






share|improve this answer





















  • This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
    – kriegaex
    Nov 25 '18 at 8:27


















0














Unfortunately no easy way to do that. Even when we have an Annotation-based pointcut, e.g.



@Aspect
@Component
@EnableAspectJAutoProxy
public class WriteTransactionAspectBean {

@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void test(org.springframework.transaction.annotation.Transactional t) {
System.out.println("TEST");
}

}


the issue is the Annotations aren't our own, they come from an external JAR (Hibernate). This would require Load-Time Weaving or some other difficult workaround.



Aspectj: intercept method from external jar



But to make things even worse, Annotations need RetentionPolicy=RUNTIME in order to be "discovered" by Pointcuts. And we would need to go thru every method and add this specification to every @Transactional. There's no way to automatically make all @Transactional's Runtime-retainable in the application.






share|improve this answer





















  • This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
    – kriegaex
    Nov 25 '18 at 8:27
















0












0








0






Unfortunately no easy way to do that. Even when we have an Annotation-based pointcut, e.g.



@Aspect
@Component
@EnableAspectJAutoProxy
public class WriteTransactionAspectBean {

@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void test(org.springframework.transaction.annotation.Transactional t) {
System.out.println("TEST");
}

}


the issue is the Annotations aren't our own, they come from an external JAR (Hibernate). This would require Load-Time Weaving or some other difficult workaround.



Aspectj: intercept method from external jar



But to make things even worse, Annotations need RetentionPolicy=RUNTIME in order to be "discovered" by Pointcuts. And we would need to go thru every method and add this specification to every @Transactional. There's no way to automatically make all @Transactional's Runtime-retainable in the application.






share|improve this answer












Unfortunately no easy way to do that. Even when we have an Annotation-based pointcut, e.g.



@Aspect
@Component
@EnableAspectJAutoProxy
public class WriteTransactionAspectBean {

@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void test(org.springframework.transaction.annotation.Transactional t) {
System.out.println("TEST");
}

}


the issue is the Annotations aren't our own, they come from an external JAR (Hibernate). This would require Load-Time Weaving or some other difficult workaround.



Aspectj: intercept method from external jar



But to make things even worse, Annotations need RetentionPolicy=RUNTIME in order to be "discovered" by Pointcuts. And we would need to go thru every method and add this specification to every @Transactional. There's no way to automatically make all @Transactional's Runtime-retainable in the application.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 19:02









gene b.gene b.

1,67352554




1,67352554












  • This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
    – kriegaex
    Nov 25 '18 at 8:27




















  • This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
    – kriegaex
    Nov 25 '18 at 8:27


















This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
– kriegaex
Nov 25 '18 at 8:27






This answer is plain wrong. The @Transactional annotation is defined with runtime scope as you can clearly see in the corresponding JavaDoc. Otherwise Spring could not do anything with it either.
– kriegaex
Nov 25 '18 at 8:27















0














You want to use a pointcut like this:



execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))





share|improve this answer





















  • The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
    – gene b.
    Nov 26 '18 at 15:12










  • I do not understand what you just wrote.
    – kriegaex
    Nov 29 '18 at 5:25
















0














You want to use a pointcut like this:



execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))





share|improve this answer





















  • The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
    – gene b.
    Nov 26 '18 at 15:12










  • I do not understand what you just wrote.
    – kriegaex
    Nov 29 '18 at 5:25














0












0








0






You want to use a pointcut like this:



execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))





share|improve this answer












You want to use a pointcut like this:



execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 8:55









kriegaexkriegaex

30.8k363100




30.8k363100












  • The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
    – gene b.
    Nov 26 '18 at 15:12










  • I do not understand what you just wrote.
    – kriegaex
    Nov 29 '18 at 5:25


















  • The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
    – gene b.
    Nov 26 '18 at 15:12










  • I do not understand what you just wrote.
    – kriegaex
    Nov 29 '18 at 5:25
















The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
– gene b.
Nov 26 '18 at 15:12




The issue I came across with this one is, I had to get a Transactional t argument to examine the object. And whenever I had this argument, the app wouldn't start (there was an error). It worked when I had a simple signature, without the actual Transactional t object.
– gene b.
Nov 26 '18 at 15:12












I do not understand what you just wrote.
– kriegaex
Nov 29 '18 at 5:25




I do not understand what you just wrote.
– kriegaex
Nov 29 '18 at 5:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381382%2fpointcut-or-aspect-around-all-service-methods-with-annotation-transactionalrea%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

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith