src\main\resources\db.properties (No such file or directory)

huangapple go评论109阅读模式
英文:

src\main\resources\db.properties (No such file or directory)

问题

Here is the translation of the code portion you provided:

我尝试将我的应用程序部署到Heroku并遇到了这个异常
Factory方法 'dataSource' 抛出了异常; 嵌套异常是java.lang.IllegalArgumentException: java.io.FileNotFoundException: src\main\resources\db.properties (没有这个文件或目录)
我尝试了不同的方法但没有找到合适的东西错误仍然困扰着我 :(

主类
```java
package com.bot.ranksystem_20.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.bot.ranksystem_20"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

这是数据库配置:

package com.bot.ranksystem_20.configuration;

import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class ConfigurationDataBase {
    @SneakyThrows
    @Bean
    public DataSource dataSource() {

        Properties properties = new Properties();
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        try {
            properties.load(new FileInputStream(new File("src\\main\\resources\\db.properties")));
            String dbUrl = properties.getProperty("db.url");
            String dbUsername = properties.getProperty("db.username");
            String dbPassword = properties.getProperty("db.password");
            String driverClassName = properties.getProperty("db.driverClassName");
            dataSource.setUsername(dbUsername);
            dataSource.setPassword(dbPassword);
            dataSource.setUrl(dbUrl);
            dataSource.setDriverClassName(driverClassName);

        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }

        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

这是我的db.property:

db.username=-
db.password=-
db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://-:5432/-

这是我长时间受苦的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.bot</groupId>
	<artifactId>ranksystem_2.0</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ranksystem_2.0</name>
	<description>Rank System for Discord Server</description>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<logentries-appender>RELEASE</logentries-appender>
		<spring.version>3.3.2.RELEASE</spring.version>
	</properties>

	<repositories>
		<repository>
			<id>jcenter</id>
			<name>jcenter-bintray</name>
			<url>https://jcenter.bintray.com</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>42.2.5</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>net.dv8tion</groupId>
			<artifactId>JDA</artifactId>
			<version>4.2.0_168</version>
		</dependency>
	</dependencies>

	<build>
		<resources>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>application.properties</include>
				<include>db.properties</include>
				<include>titleChannels.properties</include>
				<include>**/*.properties</include>
			</includes>
		</resource>
	</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<skip>true</skip>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>heroku-sdk-parent</artifactId>
				<groupId>com.heroku.sdk</groupId>
				<version>3.0.3</version>
				<configuration>
					<appname>ranksystem20</appname>
					<includetarget>false</includetarget>
					<includes>
						<include>${project.build.directory}/${project.build.finalName}.jar</include>
					</includes>
					<jdkversion>1.8</jdkversion>
					<processtypes>
						<web>java $JAVA_OPTS -jar ${project.build.directory}/${project.build.finalName}.jar</web>
					</processtypes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

希望这些翻译对你有所帮助!

英文:

I tried to deploy my application to Heroku and get this exception:

Factory method &#39;dataSource&#39; threw exception; nested exception is java.lang.IllegalArgumentException: java.io.FileNotFoundException: src\main\resources\db.properties (No such file or directory)

I tried different ways, but I didn't find anything suitable, the error still haunts me src\main\resources\db.properties (No such file or directory)

Main class:

package com.bot.ranksystem_20.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {&quot;com.bot.ranksystem_20&quot;})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

This is data base configuration:

package com.bot.ranksystem_20.configuration;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
@Configuration
public class ConfigurationDataBase {
@SneakyThrows
@Bean
public DataSource dataSource() {
Properties properties = new Properties();
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try {
properties.load(new FileInputStream(new File(&quot;src\\main\\resources\\db.properties&quot;)));
String dbUrl = properties.getProperty(&quot;db.url&quot;);
String dbUsername = properties.getProperty(&quot;db.username&quot;);
String dbPassword = properties.getProperty(&quot;db.password&quot;);;
String driverClassName = properties.getProperty(&quot;db.driverClassName&quot;);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(driverClassName);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

This if my db.property:

db.username=-
db.password=-
db.driverClassName=org.postgresql.Driver
db.url =jdbc:postgresql://-:5432/-

And this is my long suffering pom file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.bot&lt;/groupId&gt;
&lt;artifactId&gt;ranksystem_2.0&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;ranksystem_2.0&lt;/name&gt;
&lt;description&gt;Rank System for Discord Server&lt;/description&gt;
&lt;properties&gt;
&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
&lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
&lt;logentries-appender&gt;RELEASE&lt;/logentries-appender&gt;
&lt;spring.version&gt;3.3.2.RELEASE&lt;/spring.version&gt;
&lt;/properties&gt;
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;jcenter&lt;/id&gt;
&lt;name&gt;jcenter-bintray&lt;/name&gt;
&lt;url&gt;https://jcenter.bintray.com&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;version&gt;42.2.5&lt;/version&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.dv8tion&lt;/groupId&gt;
&lt;artifactId&gt;JDA&lt;/artifactId&gt;
&lt;version&gt;4.2.0_168&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;resources&gt;
&lt;resource&gt;
&lt;directory&gt;src/main/resources&lt;/directory&gt;
&lt;includes&gt;
&lt;include&gt;application.properties&lt;/include&gt;
&lt;include&gt;db.properties&lt;/include&gt;
&lt;include&gt;titleChannels.properties&lt;/include&gt;
&lt;include&gt;**/*.properties&lt;/include&gt;
&lt;/includes&gt;
&lt;/resource&gt;
&lt;/resources&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;repackage&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;skip&gt;true&lt;/skip&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;artifactId&gt;heroku-sdk-parent&lt;/artifactId&gt;
&lt;groupId&gt;com.heroku.sdk&lt;/groupId&gt;
&lt;version&gt;3.0.3&lt;/version&gt;
&lt;configuration&gt;
&lt;appname&gt;ranksystem20&lt;/appname&gt;
&lt;includetarget&gt;false&lt;/includetarget&gt;
&lt;includes&gt;
&lt;include&gt;${project.build.directory}/${project.build.finalName}.jar&lt;/include&gt;
&lt;/includes&gt;
&lt;jdkversion&gt;1.8&lt;/jdkversion&gt;
&lt;processtypes&gt;
&lt;web&gt;java $JAVA_OPTS -jar
${project.build.directory}/${project.build.finalName}.jar&lt;/web&gt;
&lt;/processtypes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

I will be glad to receive your suggestions!

答案1

得分: 0

在评论中提到,你应该让Spring Boot处理这些属性,将db.properties的内容放入application.properties中,这样它会被自动识别。

但如果你坚持使用手动配置,至少考虑使用稍微现代一点的方法。

@Configuration
@PropertySource("classpath:db.properties")
public class Config {

    @Value("${db.driverClassName}")
    String driverClassName;

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

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

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

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

    @Bean
    public DataSource dataSource() {
        var dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
英文:

like said in a comment you should let Spring Boot handle these properties by putting the content of db.properties into application.properties where it is picked up automatically.

But if you insist on using manual configuration at least consider using a tad bit more modern approach.

@Configuration
@PropertySource(&quot;classpath:db.properties&quot;)
public class Config {
@Value(&quot;${db.driverClassName}&quot;)
String driverClassName;
@Value(&quot;${db.url}&quot;)
String url;
@Value(&quot;${db.username}&quot;)
String username;
@Value(&quot;${db.password}&quot;)
String password;
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource dataSource() {
var dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}

huangapple
  • 本文由 发表于 2020年7月31日 22:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63193880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定