gpt4 book ai didi

java - java中application.properties文件的问题

转载 作者:行者123 更新时间:2023-12-02 03:40:24 25 4
gpt4 key购买 nike

我试图从属性中获取数据库值,因此出于同样的原因,我在 spring 中使用 @PropertySource,但它抛出了 FileNotFoundException

@Configuration
@EnableJpaRepositories(basePackages = {
"com.manju.springdata.repository"
})
@EnableTransactionManagement
@EnableWebMvc
@ComponentScan(basePackages = "com.manju.springdata.*")
@PropertySource("classpath:/application.properties")
public class PersistenceContext {

@Value("${db.driver}")
private String dbDriver;

@Value("${db.url}")
private String dbURL;

@Value("${db.username}")
private String dbUserName;

@Value("${db.password}")
private String dbPassword;

@Bean(destroyMethod = "close")
DataSource dataSource(Environment env){
BoneCPDataSource dataSource = new BoneCPDataSource();
//dataSource.setDriverClass(env.getRequiredProperty("db.driver"));
dataSource.setDriverClass(dbDriver);
dataSource.setJdbcUrl(dbURL);
dataSource.setUsername(dbUserName);
dataSource.setPassword(dbPassword);
return dataSource;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

我的项目结构如下,

enter image description here

我收到以下错误,

Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:84) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:360) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:254) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:231) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
... 57 common frames omitted

如何访问我的属性文件值,我的代码有什么问题?有什么建议

最佳答案

问题出在您的文件夹结构上。 resource 文件夹应位于 main 下,而不是 java 下。看this maven项目的默认结构。移动资源文件夹或将值更改为 classpath:/resources/application.properties

关于java - java中application.properties文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36904524/

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