Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring引导中配置PropertyPlaceHolderConfigure_Java_Spring_Spring Boot - Fatal编程技术网

Java 在Spring引导中配置PropertyPlaceHolderConfigure

Java 在Spring引导中配置PropertyPlaceHolderConfigure,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个Spring-Boot应用程序,我在其中传递属性源,如: @PropertySource({"classpath:application.properties","file:C:/MyFolder/project.properties"}) 一切都很完美。但是,当我尝试以编程方式执行相同操作时,如: @Bean public PropertyPlaceholderConfigurer properties() throws URISyntaxException { Property

我有一个
Spring-Boot
应用程序,我在其中传递属性源,如:

@PropertySource({"classpath:application.properties","file:C:/MyFolder/project.properties"})
一切都很完美。但是,当我尝试以编程方式执行相同操作时,如:

@Bean
public PropertyPlaceholderConfigurer properties() throws URISyntaxException {
  PropertyPlaceholderConfigurer proprty =  new PropertyPlaceholderConfigurer();
  proprty.setLocations(new Resource[] {
                                        new ClassPathResource("application.properties"),
                                        new FileSystemResource("C:/MyFolder/project.properties")});
  return proprty;
}
它不起作用。它找不到在
C:/MyFolder/project.properties
中定义的属性。有什么想法吗

我得到的线索是

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.utilisoft.backfill.Application.main(Application.java:108)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 27 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 39 more
Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)

显然,我无法确定数据库设置。它们在
project.properties
文件中定义,当我以编程方式设置属性时,该文件不会连接

如果您检查DatasourceProperties.java,您将看到配置是使用ConfigurationProperties注释绑定的。中记录了此注释的处理过程。上面提到的解决步骤都不依赖于您提供的实现。

我认为您可以通过以下方式实现所需的功能,而无需注释或以编程方式定义注释:

Spring Boot使用一个非常特殊的PropertySource顺序,该顺序旨在允许合理地重写值,属性按以下顺序考虑:

  • 命令行参数
  • SPRING_应用程序_JSON的属性(嵌入在环境变量或系统属性中的内联JSON)
  • 来自java的JNDI属性:comp/env
  • Java系统属性(System.getProperties()
  • 操作系统环境变量
  • 仅在random.*中具有属性的RandomValuePropertySource
  • 打包jar之外的特定于概要文件的应用程序属性(应用程序-{Profile}.properties和YAML变体)
  • jar中打包的特定于概要文件的应用程序属性(应用程序-{Profile}.properties和YAML变体)
  • 打包jar之外的应用程序属性(Application.properties和YAML变体)
  • 打包在jar中的应用程序属性(Application.properties和YAML变体)
  • @@Configuration类上的PropertySource注释
  • 默认属性(使用SpringApplication.setDefaultProperties指定)

  • 换句话说,如果在
    src/main/resources
    中有一个
    application.properties
    文件用于开发(上文第10点),在
    jar
    文件旁边有一个外部文件用于生产(上文第9点),正确的属性将被自动拾取。

    您遇到了生命周期问题……您正在创建一个具有尚未设置的属性的bean(如您所知)。用
    @PostConstruct
    注释的方法初始化数据库。见本段。简而言之,它表示
    初始化bean
    接口:

    …允许bean在所有必要的操作之后执行初始化工作 容器已经设置了bean的属性


    然后,它接着说使用
    @PostConstruct
    而不是
    初始化bean
    接口,这样您的代码就不会不必要地与Spring耦合。

    尝试新的FileSystemResource(“C:\\MyFolder\\project.properties”);为什么?SpringBoot已经提供了这种开箱即用的支持。仅为特定的
    项目添加
    @PropertySource
    。属性就足够了。为什么让它更复杂?@prem kumar它不起作用你提供的对我来说很好。请澄清你所说的“不起作用”是什么意思,并提供一个答案。为什么?似乎是一个丑陋的装置,不管Spring Boot已经为您加载了
    应用程序.properties
    。而不是注册您自己的占位符(这可能会干扰Spring Boot one,Spring Boot one反过来也是一个
    属性资源占位符配置器
    而不是
    属性PlaceHolderConfigure
    )。您可以从命令行传入要加载的其他属性文件,如果需要更复杂的内容,请添加
    ApplicationContextInitializer
    ,并将文件添加到
    环境上的
    PropertySources
    集合中。使用框架而不是反对它。PropertyPlaceHolderConfigure
    impls如何归入此列表?