更改属性文件的名称会导致找不到文件

时间:2019-06-17 14:37:26

标签: java spring-boot

我有一个名为 application.properties 的属性文件:

mysql.connectionUrl=jdbc:somesql://index.mycust.cr:0000/dbName
mysql.username=me
mysql.password=mypass

我的班级 DbNameConnectionProperties.java 使用它,如下所示:

package com.mycom.mycust.somesql.dbconnection;

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties("mysql")
public class DbNameConnectionProperties {

    private String connectionUrl;
    private String username;
    private String password;

    public String getConnectionUrl() {
        return connectionUrl;
    }

    public void setConnectionUrl(String connectionUrl) {
        this.connectionUrl = connectionUrl;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

运行正常。现在,我想更改 application.properties 文件的名称(对于另一个数据库,我将需要另一个具有属性的文件)。因此,我只需将 application.properties 的文件名更改为 something.properties ,以及@PropertySource批注中的名称:

something.properties

mysql.connectionUrl=jdbc:somesql://index.mycust.cr:0000/dbName
mysql.username=me
mysql.password=mypass

DbNameConnectionProperties.java

package com.mycom.mycust.somesql.dbconnection;

@Configuration
@PropertySource("classpath:something.properties")
@ConfigurationProperties("mysql")
public class DbNameConnectionProperties {

    // same as before
}

此崩溃:

2019-06-17 16:34:20,593 [ ERROR ] o.s.b.SpringApplication:842 : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.findwise.svenskaakademin.processing.Processing]; nested exception is java.io.FileNotFoundException: class path resource [something.properties] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at com.findwise.svenskaakademin.processing.Processing.main(Processing.java:37)
Caused by: java.io.FileNotFoundException: class path resource [something.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
    at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59)
    at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67)
    at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37)
    at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:453)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:272)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:194)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170)

为什么?

1 个答案:

答案 0 :(得分:1)

看来something.properties不在类路径中。仔细检查它是在资源目录中创建的。