java - Spring Boot JPA 获取对 EntityManagerFactory 的引用

标签 java spring spring-mvc jpa spring-boot

我有一个 Spring-Boot Web 应用程序,并且有一个具有数据访问层的第 3 方 jar。为了使用它,我必须向它发送对 EntityManagerFactory 的引用。我可以这样做:

    Map<String, String> stg = new HashMap<String, String>();
    stg.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydb");
    stg.put("hibernate.connection.username", "name");
    stg.put("hibernate.connection.password", "password");

    EntityManagerFactory fact = MultipleEntityManagerFactoryHolder.getEmf("stg", stg);

但是当我将其添加到 application.properties 时,如何获得 Spring(我假设)已经为我构建的属性:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username=user
spring.datasource.password=password

我无法访问该 jar,因此我需要工厂,而不是数据源。有人知道如何获得吗?

最佳答案

您可以简单地在 bean 中 Autowiring 它:

@Autowired
private EntityManagerFactory emFactory;

关于java - Spring Boot JPA 获取对 EntityManagerFactory 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46680088/

相关文章:

spring - 如何使用@Autowired像工厂模式一样动态注入(inject)实现

java - 更新 spring webflow 中的对象未显示在 UI 上

java - Spring Global CORS 配置不起作用,但 Controller 级别配置起作用

用于存储过程后端的 Java Web 框架前端

java - 数字签名 : security issue between signing on client or on server?

java - 使用 Java 有选择地解析日志文件

java - 从类中获取bean id

java - 如何在 url 请求中发送数组

java - 无法在 Java 中执行 "delete"查询

Java 正则表达式以一个反斜杠开头