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:
dependency:io.micronaut.spring:micronaut-spring[gradleScope="implementation"]
This also requires adding the spring-annotation
module dependency as an annotation processor:
dependency:io.micronaut.spring:micronaut-spring-annotation[scope="annotationProcessor"]
Note
|
If you use Micronaut’s Hibernate support you already get this dependency and a HibernateTransactionManager is configured for you.
|
This is done by intercepting method calls annotated with Spring’s @Transactional
with TransactionInterceptor.
The benefit here is you can use Micronaut’s compile-time, reflection-free AOP to declare programmatic Spring transactions. For example:
import org.springframework.transaction.annotation.Transactional;
...
@Transactional
public Book saveBook(String title) {
...
}