0

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?

2
  • You can write a setter() metod for your static field and use @Value annotation with it. Example is here : baeldung.com/spring-inject-static-field Commented Apr 8, 2020 at 4:29
  • @OrçunÇolak No point in even making that stuff static to begin with since the class will only be instantiated once. Commented Apr 8, 2020 at 17:04

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Removed static, All values are still null.
@zohebsiddiqui did you use EnableConfigurationProperties or declare your PropertySourcesPlaceholderConfigurer bean?
Actually, changing them to non static fixed it. I forgot to rebuild my Jar. Thank you for your help.
It is not the "@PropertySource" not working it static fields. "@Value" annotation does not work with static fields. See : baeldung.com/spring-inject-static-field

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.