我有一个 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/