春季配置文件未从application.properties中读取值。

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

Spring Configuration file not reading values from application.properties

问题

以下是翻译好的内容:

我的配置文件起始部分,路径为 src/main/java/{包名}/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;
}

我的应用程序属性文件,路径为 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

我正在使用 Gradle 构建项目。我将各项内容打包成一个 JAR 文件,并使用以下命令运行 JAR 文件:$java -jar /path/to/jar。代码能够执行并运行,所有的打印调试语句都被记录。然而,我的变量值全部为 null,即属性文件中的内容未被读取。

我已经确保 application.properties 文件在 JAR 包中。

存档文件 integrity-check-service.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

(此处省略了其他文件。)

有人知道为什么会发生这种情况吗?

英文:

The start of my Configuration file with path 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:

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. I convert things into a JAR and run the JAR as $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 i.e. things are not being read from the properties file.

I have made sure that the application.properties file is in the JAR.

Files in archive integrity-check-service.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?

答案1

得分: 2

@PropertySource在静态变量中不起作用。去掉static关键字。

英文:

@PropertySource doesn't work with static variables. Get rid of the static keyword.

huangapple
  • 本文由 发表于 2020年4月8日 07:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61090769.html
匿名

发表评论

匿名网友

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

确定