简体   繁体   English

Spring启动时的TransactionManager和数据源 - 弹簧数据

[英]TransactionManager and datasource in spring boot - spring data

I wanted to access the Transaction Manager and datasource in my configuration in spring boot application. 我想在Spring启动应用程序中访问配置中的事务管理器和数据源。 I am using spring-boot-starter-data-jpa artifact. 我正在使用spring-boot-starter-data-jpa工件。

Is it possible to just autowire in the config and get its access? 是否可以在配置中自动装配并获取其访问权限?

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: 对于DataSource ,您选择的启动器开箱即用,您将获得tomcat-jdbc数据源:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-configure-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. 确保使用JDBC DataSource类型( javax.sql.DataSource ),而不是特定的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM