Pinchy
Pinchy

Reputation: 1536

Migrating from Spring configuration from XML file to Annotation

<prop key="load*">PROPAGATION_REQUIRED, readOnly</prop>

Here the key="load*" uses wildcard "*". It matches every function that starts with load right? My question is that; how am I going to implement this using annotation based configuration without placing it on every function?

@Transactional(readOnly = true, propagation= Propagation.REQUIRED)

Is there anyway I can do this without putting this on top of every function which it's name starts with "load"?

Cheers

Upvotes: 2

Views: 95

Answers (1)

Sergey Makarov
Sergey Makarov

Reputation: 2531

You can use aspects for this purpose. I.e. define an aspect which intercepts all methods named as "load*" and ensure transaction is started. However in my opinion, such approach is a bad one since it makes your code hard to unit test, understand and maintain further. In fact @Transactional handling is done using aspects, so why bothering to add another layer of abstraction to this? With this annotation someone will look to your code and immediately know that method is/should be running in transaction. Hiding this will make others life harder.

If you still want to implement it, here is small example. NOTE, that I didn't test this at all, just an idea demonstration.

@Aspect
public class MyInterceptor
{
   @Around("execution(* load*(..))")
   @Transactional
   public Object makeMeTransactional(ProceedingJoinPoint pjp)
   {
      return pjp.proceed();
   }
}

Upvotes: 1

Related Questions