TransactionManager and datasource in spring boot - spring data
Asked Answered
H

1

5

I wanted to access the Transaction Manager and datasource in my configuration in spring boot application. I am using spring-boot-starter-data-jpa artifact.

Is it possible to just autowire in the config and get its access?

Hakodate answered 22/4, 2016 at 12:28 Comment(0)
V
11

You can get access to the transaction manager with:

@Autowired
private PlatformTransactionManager transactionManager;

For the DataSource, out-of-the-box with the starter you chose you will get the tomcat-jdbc datasource:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-configure-datasource

You can just inject that like this:

@Autowired
private DataSource dataSource;

Make sure you use the JDBC DataSource type (javax.sql.DataSource), and not a specific implementation.

Vibratile answered 22/4, 2016 at 12:31 Comment(4)
Thanks leeor, code in general works worked, but IntelliJ complains for autowiring saying "No Beans of 'DataSource' type found. And same for transaction managerHakodate
Hmm, what version of intellij? newer versions have better support for spring-boot. You might also be able to make that go away by adding spring facets to your module under project structure.Vibratile
It is 15.0.1 ultimate, and I tried adding spring facets but it is still there. It might be because I may not doing it properly.Hakodate
I think v2016 added more boot support blog.jetbrains.com/idea/2016/02/… not sure that would fix it but could.Vibratile

© 2022 - 2024 — McMap. All rights reserved.