Pointcut or Aspect Around All Service Methods with Annotation @Transactional(readOnly = false)
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
add a comment |
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
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
add a comment |
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
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
java spring aop aspectj pointcut
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
You want to use a pointcut like this:
execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))
The issue I came across with this one is, I had to get aTransactional 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 actualTransactional 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
add a comment |
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
});
}
});
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%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
You want to use a pointcut like this:
execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))
The issue I came across with this one is, I had to get aTransactional 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 actualTransactional 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
add a comment |
You want to use a pointcut like this:
execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))
The issue I came across with this one is, I had to get aTransactional 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 actualTransactional 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
add a comment |
You want to use a pointcut like this:
execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))
You want to use a pointcut like this:
execution(@org.springframework.transaction.annotation.Transactional(readOnly = false) * com.app.service..*.*(..))
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 aTransactional 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 actualTransactional 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
add a comment |
The issue I came across with this one is, I had to get aTransactional 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 actualTransactional 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
add a comment |
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.
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%2f53381382%2fpointcut-or-aspect-around-all-service-methods-with-annotation-transactionalrea%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
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