【问题标题】:Spring Boot JPA get reference to EntityManagerFactorySpring Boot JPA 获取对 EntityManagerFactory 的引用
【发布时间】:2018-03-22 15:34:17
【问题描述】:

我有一个 Spring-Boot Web 应用程序,并且我有一个具有数据访问层的 3rd 方 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,所以我需要工厂,而不是 DataSource。有谁知道如何获得它?

【问题讨论】:

  • 我理解你的问题吗?您想使用基于属性文件条目的实体管理器将属性添加到您的地图吗?

标签: java spring spring-mvc jpa spring-boot


【解决方案1】:

您可以简单地在您的 bean 中自动装配它:

@Autowired
private EntityManagerFactory emFactory;

【讨论】:

  • 我真的需要记住,我需要的大部分东西已经在 Spring Boot 中了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-01-09
  • 2018-02-21
  • 2017-12-12
  • 1970-01-01
  • 2018-04-02
  • 2019-02-20
  • 2023-03-03
  • 2016-05-30
相关资源
最近更新 更多