gpt4 book ai didi

java - 更改属性文件的名称导致找不到它

转载 作者:行者123 更新时间:2023-11-30 10:02:46 24 4
gpt4 key购买 nike

我有一个名为 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)

为什么?

最佳答案

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

关于java - 更改属性文件的名称导致找不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com