This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi friends,
I have a question regarding spring transaction using AOP.
I can able to declare an transaction advice in spring.xml and able to apply the advice to respective methods using pointcut.
Here transactionAdvice(txAdvice) is applied to all methods starting with book present in org.simple.service.TicketBookingService.
But,
I would like to use annotation.So what i did is
This is also working fine.
Now what I feel is,here @Transactional is repeated in all methods which is starting with book.So i would like to put in some common area.
So I did the following,
1.
in spring.xml,by mentioning this spring will scan for aspect components.
2.
I created a class called TransactionLoggingAspect.
My question is,
what is the annotation equivalent of the above "txAdvice"?
In TransactionLoggingAspect.java,Where I can put the annotation equivalent?
To use @Transactional Annotations. Your config has to have <tx:annotation-driven/> And you need to have a transactionManager bean. That is all. Now you can put @Transactional on any service method and it will be transactional.
No need for any aop or aspect classes. Everything that you did before in xml is taken care of. That is why using annotations for Transactional methods is so much easier than using xml for it.
Mark Spritzler wrote:To use @Transactional Annotations. Your config has to have <tx:annotation-driven/> And you need to have a transactionManager bean. That is all. Now you can put @Transactional on any service method and it will be transactional.
No need for any aop or aspect classes. Everything that you did before in xml is taken care of. That is why using annotations for Transactional methods is so much easier than using xml for it.
Mark
Hi Mark Spritzler,
Thanks for your comment. I understood your comment and totally agree with that.
My question here is,
I keep on put @Transactional for any service methods that i needed.I feel lazy to put @Transactional also.
Instead I would like to put in some common area @Transactional and declare that methods starting with book*(bookBusTicket,bookPlaneTicket,bookTrainTicket) present in org.simple.service.TicketBookingService class.
This I can put in xml as
But I would like in programmatic way.(@Transactional in common area and declare methods starting with book*(bookBusTicket,bookPlaneTicket,bookTrainTicket) present in org.simple.service.TicketBookingService class)
Is there any programmatic way?(May be annotation or something)
(My question may be wrong.correct me if i am wrong)
Well, you can put @Transactional on the class level and then all methods from your interfaces that that class implements are now all transactional.
That is a way to put the annotation in once and get many methods covered.
So one trick it to make sure you put all those methods that start with into a single interface that that class implements. Or just put @Transactional on that interface.
Mark
Anand Sivathanu
Ranch Hand
Joined: Jun 25, 2010
Posts: 76
posted
0
Mark Spritzler wrote:Well, you can put @Transactional on the class level and then all methods from your interfaces that that class implements are now all transactional.
That is a way to put the annotation in once and get many methods covered.
So one trick it to make sure you put all those methods that start with into a single interface that that class implements. Or just put @Transactional on that interface.
Mark
Oh.
You are telling to put @Transaction in the class level or interface level
1.
class level - flavor 1
2.
interface level - flavor 2
These are the two flavors you are coming to say. Am I right?