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?

leeor
  • 17,041
  • 6
  • 34
  • 60
krmanish007
  • 6,749
  • 16
  • 58
  • 100

1 Answers1

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.

leeor
  • 17,041
  • 6
  • 34
  • 60
  • Thanks leeor, code in general works worked, but IntelliJ complains for autowiring saying "No Beans of 'DataSource' type found. And same for transaction manager – krmanish007 Apr 22 '16 at 13:38
  • 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`. – leeor Apr 22 '16 at 13:52
  • 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. – krmanish007 Apr 22 '16 at 14:12
  • I think v2016 added more boot support http://blog.jetbrains.com/idea/2016/02/intellij-idea-16-spring-boot-and-kotlin-rc/ not sure that would fix it but could. – leeor Apr 22 '16 at 14:14