简体   繁体   English

Spring 配置文件未从 application.properties 读取值

[英]Spring Configuration file not reading values from application.properties

The start of my Configuration file with path src/main/java/{package name}/DataBaseConfiguration.java:我的配置文件的开头是路径 src/main/java/{package name}/DataBaseConfiguration.java:

@Configuration
@PropertySource("classpath:application.properties")
public class DataBaseConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger("DataBaseConfiguration.class");

    @Value("${tenantdb.driver.classname}")
    static String tenantDbDriverClassname;

    @Value("${tenantdb.url}")
    static String tenantDbUrl;

    @Value("${tenantdb.username}")
    static String tenantDbUsername;

    @Value("${tenantdb.password}")
    static String tenantDbPassword;

My application.properties file with path src/main/resources/application.properties:我的 application.properties 文件的路径为 src/main/resources/application.properties:

tenantdb.driver.classname=org.postgresql.Driver
tenantdb.url=jdbc:postgresql://127.0.0.1:5000/tenant_db
tenantdb.username=tenant_admin_user
tenantdb.password=postgres
tenantdb.connectionpool.initialSize=3
tenantdb.connectionpool.maxActive=25
tenantdb.connectionpool.maxIdle=25
tenantdb.connectionpool.minIdle=1

I am using gradle to build the project.我正在使用 gradle 来构建项目。 I convert things into a JAR and run the JAR as $java -jar /path/to/jar.我将事物转换为 JAR 并将 JAR 作为 $java -jar /path/to/jar 运行。 The code executes and runs and all my print debugging statements are logged.代码执行并运行,并记录了我的所有打印调试语句。 However the values of my variables are all null ie things are not being read from the properties file.但是我的变量的值都是 null 即没有从属性文件中读取内容。

I have made sure that the application.properties file is in the JAR.我已确保 application.properties 文件位于 JAR 中。

Files in archive integrity-check-service.jar存档完整性检查服务中的文件。jar

META-INF/
META-INF/MANIFEST.MF
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/com/
BOOT-INF/classes/com/agilysys/
BOOT-INF/classes/com/agilysys/analytics/
BOOT-INF/classes/com/agilysys/analytics/integritycheckservice/
BOOT-INF/classes/com/agilysys/analytics/integritycheckservice/CompareData.class
BOOT-INF/classes/com/agilysys/analytics/integritycheckservice/DataBaseConfiguration.class
BOOT-INF/classes/application.properties

(There are other files but I haven't printed them all here.) (还有其他文件,但我没有在这里全部打印出来。)

Does anyone know why this is happening?有谁知道为什么会这样?

@PropertySource doesn't work with static variables. @PropertySource 不适用于 static 变量。 Get rid of the static keyword.去掉 static 关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM