【问题标题】:Error while reading property file using @PropertySource in spring boot在 Spring Boot 中使用 @PropertySource 读取属性文件时出错
【发布时间】:2018-02-15 16:28:47
【问题描述】:

我是 Spring-Boot 应用程序的新手,并试图在我的 Spring Boot 应用程序中读取外部属性文件,该文件位于我的 resource 文件夹中,除了 application.properties,但出现以下异常,

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.test.config.MyInetgrationApplicationConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/myIntegration.properties]
2017-09-06 17:27:04.866 ERROR 10512 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@225fa519: startup date [Wed Sep 06 17:27:04 IST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

我的Config类如下,

@Configuration
@PropertySource(name="myIntegrationProperties", value ="myIntegration.properties")
public class MyInetgrationApplicationConfig {

    /**
     * Rest template.
     *
     * @return the rest template
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Value("${datasource.name}")
    private String dataSourceName;

    @Value("${datasource.driver}")
    private String dataSourceDriver;

    @Value("${datasource.url}")
    private String dataSourceUrl;

    @Value("${datasource.username}")
    private String dataSourceUserName;

    @Value("${datasource.password}")
    private String dataSourcePassword;

    @Bean
    @Primary
    public DataSource dataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setName(dataSourceName);
        dataSource.setUrl(dataSourceUrl);
        dataSource.setDriverClassName(dataSourceDriver);
        dataSource.setUsername(dataSourceUserName);
        dataSource.setPassword(dataSourcePassword);

        return dataSource;
    }
}

我的应用程序类,

@SpringBootApplication(scanBasePackages = { "com.test" })
public class MyInetgrationApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyInetgrationApplication.class);
    }

}

【问题讨论】:

    标签: java spring spring-boot properties


    【解决方案1】:

    你需要添加claapath:

    @PropertySource("classpath:myIntegration.properties")
    

    myIntegration.properties 文件应放在 /src/main/resources 下,以便在运行时在 classpath 上可用。

    【讨论】:

    • 我已经尝试通过 aading "classpath:" 但再次得到以下异常,BeanCreationException:创建名为 'myApplicationConfig' 的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${datasource.driver} 中的占位符“datasource.driver”
    • 该错误与myApplicationConfig 有关,与PropertySource 无关。发布您的myApplicationConfig 代码。
    • mkyong.com/spring/spring-propertysources-example 在这里你可以得到它的所有深入细节。
    • 抱歉错字,仅限 MyInetgrationApplicationConfig。 BeanCreationException:创建名为“myInetgrationApplicationConfig”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException: Could not resolve placeholder 'datasource.driver' in value "${datasource.driver} 我应该排除一些数据源的自动配置吗?
    • 您是否将@ComponentScan("your structure") 添加到您已实现main 方法的类中。
    【解决方案2】:

    尝试为您的资源文件路径提供类路径引用,如下所示:

    @PropertySource(value = { "classpath:myIntegration.properties" }, name="myIntegrationProperties")
    

    【讨论】:

      【解决方案3】:

      我无法使用@Value("${datasource.name}") 获取值,所以我在自动装配Environment 之后尝试过,然后使用environment.getProperty("datasource.name") 它对我有用。 以下是示例代码,

          @Configuration
          @PropertySource("classpath:myIntegration.properties")
          public class MyInetgrationApplicationConfig {
      
              @Autowired
              private Environment environment;
      
              @Bean
              @Primary
              public DataSource dataSource() {
                  DataSource dataSource = new DataSource();
                  dataSource.setName(environment.getProperty("datasource.name"));
                  dataSource.setUrl(environment.getProperty("datasource.url"));
                  .
                  .
                  .
                  .
                  return dataSource;
              }
         }
      

      【讨论】:

        猜你喜欢
        • 2016-04-29
        • 2021-11-22
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 2018-09-28
        • 2019-01-11
        相关资源
        最近更新 更多