简体   繁体   中英

Spring boot and EntityManager based on application.properties

I have following configuration file for Spring boot application.properties :

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver    
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true  
spring.jpa.generate-ddl=true 
spring.jpa.hibernate.ddl-auto = update 
spring.jpa.database=POSTGRESQL   
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect

It perfectly handled by spring ddl-auto creates tables for me. I expect it's enough information to create EntityManager from this 'DataSource'.

I found out that one of the ways to create EntityManager is:

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "DataSorceGoesHere" );    
EntityManager entitymanager = emfactory.createEntityManager( );

The questions are:

  1. What is the name of my datasource configured in application.properties ?
  2. Is there any opportunities to inject EntityManager based on application.properties ?

for the name of the datasource I think that we can show it with the name of all the beans created with spring boot with this code

public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("List of beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.print(beanName);
            System.out.print(" ");
        }

        System.out.println("");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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