How to control spring #transactional Annotation precedence? - spring

The situation is that I am using Aspectj with spring aop,I define a aspect(After advice) to do some data opration for a method.And I also use the spring #transactional for the same method.
Now the question comes,how can I control the order for them to aspectj?
(I know how to control the two aspect defined by myself ,but I have no idea if one of them is spring #transactional )
Thank you very much for your help.

You can define relative order of transactional aspect using order attribute of <tx:annotation-driven>, it works the same way as orders of other aspects.

Related

Spring Boot : how auto configure works and #JsonTest

I've read some stuff about how auto-configuration works behind the scene (configuration classes with #Conditional, spring.factories inside /META-INF etc...)
Now I'm trying to understand with an example : #JsonTest
I can see this annotation is annotated with things like #AutoConfigureJson
What this #AutoConfigureJson does exactly ? Does it import some configuration classes with beans inside ? How Spring know how to use this annotation (basically this annotation is almost empty and doesn't say which classes to scan)
#AutoConfigure... (like #AutoConfigureJson) annotations are the way to allow tests with multiple "slices".
Slices load into your tests only a subset of the application, making them run faster. Let's say you need to test a component that uses the Jackson Object Mapper, then you would need the #JsonTest slice. (here is the list of all available slices.)
But you may also need some other part of the framework in your test not just tha single slice; let's say the JPA layer. You may want to annotate the test with both #JsonTest and #DataJpaTest to load both slices. According to the docs, this is not supported.
What you should do instead is choose one of the#...Test annotation, and include the other with an #AutoConfigure... annotation.
#JsonTest
#AutoConfigureDataJpa
class MyTests {
// tests
}
Update:
at a certain point while evaluating the annotation, Spring Boot will hit this line and will pass to the method SpringFactoriesLoader.loadFactoryNames() a source, that is the fully qualified name of the annotation (like interface org.springframework.boot.test.autoconfigure.json.AutoConfigureJson for example).
The loadFactoryNames method will do its magic and read the necessary information from here.
If more details are needed, the best thing is to use a debugger and just follow along all the steps.

Spring dynamic wiring

In the case of wiring in Spring (using the qualifier annotation), why spring does not allow the qualifier to be a dynamic value ?
For example I want to inject a notifier implemetation object in a person object based on a value found in the person object.
what would be the best solution for such situation?
thanks

Spring AOP Prototype-scoped Aspects are Firing out-of-order

I am using a set of Spring AOP Aspects (mostly from my library here). I am finding that the ordering I specify for the aspects is no longer being respected (I am certain that, at some point in the past, say, 1 year ago on Boot 1.3.x, it was respected) when the scope of the aspect bean is "prototype". If I remove the 'scope="prototype"' in XML, or the #Scope("prototype") in JavaConfig, the ordering is correct, but when the scope is prototype the ordering does not work - the aspects fire in apparently random order. The aspects implement the Ordered interface.
Bean definitions follow the pattern (JavaConfig):
#Bean
#Scope("prototype")
public CircuitBreakerAspect circuitBreakerAspect()
{
CircuitBreakerAspect aspect = new CircuitBreakerAspect();
aspect.setGraphiteClient(graphiteClient);
aspect.setOrder(100);
return aspect;
}
I need the aspects to be prototype scope, because some of them (e.g., RetryInterceptor) are stateful (maintaining a count of failed operations, which is exported to JMX). If I remove the prototype scope, the ordering works correctly but the same singleton aspect instance is used for all advised bean instances!
I am on Spring Boot 1.4.1 and Java 8.
How can I get prototype aspects to order correctly?
I don't think that the prototype scope is supported for aspects or more likely it does not make sense. Documentation at 11.2.6 Aspect instantiation models specifically mentions this:
By default there will be a single instance of each aspect within the
application context.
To modify this behaviour, Spring AOP supports AspectJ perthis and pertarget instantiation models. Perhaps they will be useful to you.
This appears to be a bug in Spring. If I add the #Order annotation to the aspects, they order properly. I've submitted a bug with Spring. https://jira.spring.io/browse/SPR-14959

How to write a advice which will inject for all the DAO classes which is annotated with #Repository

When I tried to find the answer for "difference between #component,#service,#Repository,#controller" I found one additional answer "It will help in case of AOP when you separate the layers by using these annotation.
For example if you want to inject one Aspect of logging (before,after)to all the DAO,you are able to do that using AOP as you have three distinct Layers and are not mixed.
Can anyone please suggest me the sample code how to write join points which will apply to all #Repository by using this annotation.I know how to create joint points with the help of expressions,autoproxycreater(which will tell inject advice whose method matches the advisor's pointcut) but how we will tell to inject advice to all the classes which is annotated with #Repository.
The following pointcut expression to advice based on annotation #target(org.springframework.stereotype.Repository) should help.

Can I inject code in spring using AOP annotations?

Is it possible to do something like the following:
public void doStuff(#RequirePrivilege("foo") User user) {
// ...
}
and have it effectively run as if it were the following?
public void doStuff(User user) {
if(!user.hasPrivilege("foo"))
throw new UserHasInsufficientPrivileges(); // this is a RuntimeException
// ...
}
I know that Spring has various sorts of AOP support, but the best I could find was AOP code which was annotated so that it would execute before or after a specific method. I want to do the inverse and annotate the code that should be changed.
Ultimately I could just do the above check inside the method, but the annotation way of doing things provides additional documentation which makes it obvious that the user requires a particular privilege without having to keep the documentation in sync with the code.
You can look at using AspectJ for doing this, as it will match on annotations. You can then use an around aspect to decide if the user meets the requirements to use this method.
Spring allows you to use AspectJ, and I would suggest that if possible you not do this at run-time, but at compile-time, as there is no reason to pay the price for using this aspect whenever you start the application. But, if you must do it at run-time then that is doable, to me I try to use compile-time as much as possible.
You may want to look at AspectJ In Action (http://www.manning.com/laddad2/) but here is an example from there:
Signature pattern:
* *(#RequestParam
(#Sensitive *))
Description
*Any method with one parameter marked with the #RequestParam annotations and the parameter’s type is marked with the #Sensitive annotation.*
Example
void create(#RequestParam
MedicalRecord mr), assuming
MedicalRecord carries the
#Sensitive annotation.
I'm certain that your "insufficient privileges" example can be done with Spring AOP, because that's how Spring Security works. You can do some very sophisticated things with around advice and AspectJ.

Resources