系统 属性 移动到 application.properties 时停止生效

System property stops taking effect when moved to application.properties

我有一个 Spring 启动应用程序,它使用 Spring 数据和 Hibernate 进行数据访问。我最初遇到过很长的应用程序启动时间。所以,基于这个answer,我把'hibernate.temp.use_jdbc_metadata_defaults'设置为'false'作为系统属性(-D)。应用程序启动时间问题已解决。

现在,我将该配置从系统 属性 移至 application.properties。延迟的应用程序启动问题又回来了。

我的application.properties:

hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.validation-query=SELECT 1
spring.datasource.testOnBorrow=true
spring.datasource.testWhileIdle=true

将 "org.springframework.core.env" 设置为 "DEBUG" 日志记录后,我看到 属性 已从 application.properties 解析(如预期)。

INFO   | jvm 1    | 2015/07/20 19:47:15 | 19:47:15.211 [WrapperJarAppMain] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.jpa.database-platform' in [applicationConfig: [classpath:/application.properties]] with type [String] and value 'org.hibernate.dialect.PostgreSQLDialect'
...
INFO   | jvm 1    | 2015/07/20 19:47:15 | 19:47:15.226 [WrapperJarAppMain] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'hibernate.temp.use_jdbc_metadata_defaults' in [applicationConfig: [classpath:/application.properties]] with type [String] and value 'false'
...
INFO   | jvm 1    | 2015/07/20 19:47:15 | 19:47:15.229 [WrapperJarAppMain] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.jpa.hibernate.ddl-auto' in [applicationConfig: [classpath:/application.properties]] with type [String] and value 'none'

在延迟启动期间,应用程序会在特定点停止然后恢复。当它停止时,"jstack"ing JVM 给出以下结果:

INFO   | jvm 1    | 2015/07/20 19:47:58 | "WrapperJarAppMain" #20 prio=5 os_prio=0 tid=0x00007fb1ec02b800 nid=0x1d39 runnable [0x00007fb248257000]
INFO   | jvm 1    | 2015/07/20 19:47:58 |    java.lang.Thread.State: RUNNABLE
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.net.SocketInputStream.socketRead0(Native Method)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.net.SocketInputStream.read(SocketInputStream.java:170)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.net.SocketInputStream.read(SocketInputStream.java:141)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:143)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:112)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:71)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:282)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1803)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       - locked <0x000000077a9f8ca0> (a org.postgresql.core.v3.QueryExecutorImpl)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.TypeInfoCache.getPGType(TypeInfoCache.java:323)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       - locked <0x000000077aa00ce0> (a org.postgresql.jdbc2.TypeInfoCache)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.TypeInfoCache.getSQLType(TypeInfoCache.java:175)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.TypeInfoCache.requiresQuoting(TypeInfoCache.java:711)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getTypeInfo(AbstractJdbc2DatabaseMetaData.java:4002)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.engine.jdbc.spi.TypeInfo.extractTypeInfo(TypeInfo.java:101)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:163)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:852)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:845)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       - locked <0x00000006dc2676d0> (a java.util.concurrent.ConcurrentHashMap)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       - locked <0x00000006dbd48650> (a java.lang.Object)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at xyz.abc.AppApplication.main(AppApplication.java:10)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.lang.reflect.Method.invoke(Method.java:497)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
INFO   | jvm 1    | 2015/07/20 19:47:58 |       at java.lang.Thread.run(Thread.java:745)
INFO   | jvm 1    | 2015/07/20 19:47:58 |

来自 "org.postgresql.jdbc2.TypeInfoCache.getSQLType"(在上面的堆栈中),我认为 Hibernate 仍在尝试加载元数据。这与我的配置相反。

如果能提供有关将配置从系统 属性 移动到 application.properties 会导致其不生效的任何指示,我将不胜感激?

要指定不在默认 spring.jpa 预期属性中的任何 属性,请使用 spring.jpa.properties.<your-property-name-here>

所以

hibernate.temp.use_jdbc_metadata_defaults=false

在您的 application.properties.

中改用这个
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

这在 Spring 引导参考指南的 this section 中有解释。

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

将以上内容添加到 spring 项目的 application.properties 文件中