5.9 Bridging Spring AOP
Although Micronaut’s design is based on a compile-time approach and does not rely on Spring dependency injection, there is still a lot of value in the Spring ecosystem that does not depend directly on the Spring container.
You may wish to use existing Spring projects within Micronaut and configure beans to be used within Micronaut.
You may also wish to leverage existing AOP advice from Spring. One example of this is Spring’s support for declarative transactions with @Transactional
.
Micronaut provides support for Spring-based transaction management without requiring Spring itself. Simply add the spring
module to your application dependencies:
implementation("io.micronaut.spring:micronaut-spring")
<dependency>
<groupId>io.micronaut.spring</groupId>
<artifactId>micronaut-spring</artifactId>
</dependency>
If you use Micronaut’s Hibernate support you already get this dependency and a HibernateTransactionManager is configured for you. |
This is done by defining a Micronaut Transactional annotation that uses @AliasFor in a manner that every time you set a value with Transactional it aliases the value to the equivalent value in Spring’s version of @Transactional
.
The benefit here is you can use Micronaut’s compile-time, reflection-free AOP to declare programmatic Spring transactions. For example:
Using @Transactional
import io.micronaut.spring.tx.annotation.*;
...
@Transactional
public Book saveBook(String title) {
...
}
Micronaut’s version of Transactional is meta-annotated with @Blocking, ensuring that all methods annotated with it use the I/O thread pool when executing within the HTTP server |