java - 在 Spring Boot 中使用 @PropertySource 读取属性文件时出错

标签 java spring spring-boot properties

我是 Spring-Boot 应用程序的新手,尝试读取 Spring Boot 应用程序中的外部属性文件,该文件位于 application.properties 之外的资源文件夹中,但出现以下异常,

enter image description here

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);
    }

}

最佳答案

您需要添加claapath:

@PropertySource("classpath:myIntegration.properties")

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

关于java - 在 Spring Boot 中使用 @PropertySource 读取属性文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46087788/

相关文章:

java - 如何在不编写并发逻辑的情况下使用 Spring 返回 Future 对象?

java - Spring ThreadPoolTask​​Executor vs Java Executorservice cachedthreadpool

spring - 如何在spring boot和嵌入式tomcat中设置mod_reqtimeout?

java - 具有组合多个角色的 Spring security 拦截 url

java - 自定义 Spring Bean 参数

java - 如何在 Java 程序中更改 Mac OS X 的 'About' 屏幕?

java - 家庭/远程接口(interface) EJB 3.1 的目的是什么

java - 配置 Spring Security 时遇到问题

java - Springboot - Spring Kafka - 惰性容器工厂初始化

mysql - 更新非主键列时出现重复条目