代码之家  ›  专栏  ›  技术社区  ›  Sahand

无法更改spring属性文件的名称

  •  0
  • Sahand  · 技术社区  · 7 年前

    我有一个名为 应用程序.属性 以下内容:

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

    这很好。现在我想更改 应用程序.属性 文件(对于另一个数据库,我需要另一个包含属性的文件)。所以我只需更改 应用程序.属性 某物、财产 ,以及 @PropertySource 注释:

    某物、财产 以下内容:

    mysql.connectionURL=jdbc:somesql://index.mycust.cr:0000/dbname
    mysql.username=我
    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)
    

    为什么?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Antoniossss    7 年前

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