我已经研究类似的问题有一段时间了,但似乎无法解决这个问题 - 这应该是直截了当的。我有一个简单的 Java 类,它将 CSV 文件导入到 PostgreSQL 表中。该类看起来像这样
@Component
public class CsvToDb {
@Value( "${spring.datasource.url}" )
String jdbcUrl;
@Value( "${spring.datasource.username}" )
String user;
@Value( "${spring.datasource.password}" )
String pass;
private static final Logger logger = LogManager.getFormatterLogger(CsvToDb.class.getName());
GetProperties prop = new GetProperties();
String queryFile = "queries/sql-queries.properties";
String import_csv = prop.getProperty(queryFile, "importCsv");
public void importCsv(File file) throws Exception {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(jdbcUrl, user, pass);
logger.info("Copying data rows from stdin...");
CopyManager copyManager = new CopyManager((BaseConnection) con);
FileReader fileReader = new FileReader(file);
copyManager.copyIn(import_csv, fileReader );
logger.info("CSV File imported to database");
}
}
我的问题是从应用程序属性文件访问数据库凭据,其中我有 3 个针对不同环境的数据库凭据 - 开发、测试和应用程序。生产。这些文件的命名类似于 application-{environment}.properties
。环境在运行时传递到应用程序中,使用当前 Windows Bat 脚本中的选项 -Dspring.profiles.active=%arg1%
(尽管它也会在 shell 脚本中被调用)还有 Linux 服务器)。我可以从日志输出中看到,所有 3 个数据库凭据都被选取为 NULL。如果我将凭据硬编码到此类中,它就可以完美工作。应用程序属性文件内容的示例如下所示
spring.datasource.url=jdbc:postgresql://...:5432/...
spring.datasource.username=username
spring.datasource.password=password
我的资源文件夹中有以下属性文件:“application-dev.properties
”、“application-test.properties”,“<代码>application-Production.properties”&amp; “application.properties
”。目前,“application.properties
”文件中的内容与“application-dev.properties
”文件中的内容相同,作为测试以查看它是否是某些内容与不拾取配置文件特定属性文件有关。当我运行bat文件时,我还在命令行中传递“dev
”,并且我可以从日志输出中看到,它确实告诉我“以下配置文件处于活动状态: dev
"
我缺少什么吗?任何关于如何从此类中的属性文件正确访问这些值的正确方向的指针将不胜感激。
I've been looking through similar questions for some time now but can't seem to resolve this issue - which should be straight forward. I have a simple Java class which imports a CSV file into a PostgreSQL table. The class looks like this
@Component
public class CsvToDb {
@Value( "${spring.datasource.url}" )
String jdbcUrl;
@Value( "${spring.datasource.username}" )
String user;
@Value( "${spring.datasource.password}" )
String pass;
private static final Logger logger = LogManager.getFormatterLogger(CsvToDb.class.getName());
GetProperties prop = new GetProperties();
String queryFile = "queries/sql-queries.properties";
String import_csv = prop.getProperty(queryFile, "importCsv");
public void importCsv(File file) throws Exception {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(jdbcUrl, user, pass);
logger.info("Copying data rows from stdin...");
CopyManager copyManager = new CopyManager((BaseConnection) con);
FileReader fileReader = new FileReader(file);
copyManager.copyIn(import_csv, fileReader );
logger.info("CSV File imported to database");
}
}
My problem is accessing the database credentials from the application property files, of which I have 3, for different environments - dev, test & production. The files are named like application-{environment}.properties
. The environment gets passed into the application at runtime, using the option -Dspring.profiles.active=%arg1%
currently in a windows bat script (although it will also be getting called in a shell script script on a Linux server also). I can see from the log output that all 3 database credentials are being picked up as NULL
. If I hard code the credentials into this class, it works perfectly. An example of the contents of the application properties file looks like this
spring.datasource.url=jdbc:postgresql://...:5432/...
spring.datasource.username=username
spring.datasource.password=password
I have the following property files in my resource folder: "application-dev.properties
", "application-test.properties
", "application-production.properties
" & "application.properties
". I currently have the same content in the "application.properties
" file as what's in the "application-dev.properties
" file, as a test to see if it was something to do with not picking up the profile specific property file. I'm also passing in "dev
" in the command line when I run the bat file, and I can see from the log output, that it does tell me "The following profiles are active: dev
"
Is there something I'm missing? Any pointers in the right direction as to how I can correctly access these values from my property files within this class would be greatly appreciated.
发布评论
评论(1)
确保Spring创建CSVTODB对象。使用“新CSVTODB()”将不起作用。
另外,您没有提及这是否是Spring Boot应用程序,但是根据属性名称和属性文件名,它看起来像我。 Spring Boot可以为您配置数据源对象,因此您不必管理代码中的属性。
Ensure that spring creates the CsvToDb object. Using "new CsvToDb()" will not work.
Also, you did not mention if this was a spring boot application but it looks like it to me based on the property names and properties files names. Spring boot can configure the Data Source object for you so that you don't have to manage the properties in your code.
https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.sql.datasource.configuration