java spring boot中的事务管理器和数据源——spring data

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36793861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:52:28  来源:igfitidea点击:

TransactionManager and datasource in spring boot - spring data

javaspringspring-boottransactionmanager

提问by krmanish007

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

我想在 Spring Boot 应用程序的配置中访问事务管理器和数据源。我正在使用spring-boot-starter-data-jpa神器。

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

是否可以在配置中自动装配并获得访问权限?

回答by leeor

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-jdbcdatasource:

对于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 DataSourcetype (javax.sql.DataSource), and not a specific implementation.

确保使用 JDBCDataSource类型 ( javax.sql.DataSource),而不是特定的实现。