Search code examples
javaspring-boottransactionsaop

Spring Transactions and AOP proxies


In many articles I read the following "Spring's declarative transaction is enabled with AOP proxies".

For a newbie like me, what does that mean exactly ?

On what kind of classes or beans can I use the @Transactional annotation ?

Do I have to add the maven dependency "spring-aop" in POM.xml ?

Is @EnableTransactionManagement explicitely needed in order to make transaction work ?


Solution

  • There is already an answer which goes into a bit of detail on what @Transactional does and how it works.

    Regarding your other questions.

    On what kind of classes or beans can I use the @Transactional annotation?

    In theory on each class you want. Should you do this no. For instance, making your web layer, the transactional boundary is generally considered as being a bad idea, as it is your service layer that should be the transactional boundary.

    Do I have to add the maven dependency spring-aop in pom.xml ?

    No. The spring-tx dependency already pulls this in itself. When using the spring-boot-starter-data-jpa (or another persistence related one) it will also be included automatically.

    Is @EnableTransactionManagement explicitly needed in order to make the transaction work?

    It depends. Generally speaking in a Spring Boot application you won't need to add this, as Spring Boot will automatically enable this. If you don't use Spring Boot but just a regular Spring application you will need to add @EnableTransactionManagement or <tx:annotation-driven> when using XML to tell Spring to process the @Transactional annotations.