I am having an issue since I moved to version 1.1.4.RELEASE of Spring Boot.
My variables that are annotated with @Value are presently not being populated with values despite them being present in the application.properties. Prior to this I was using Spring Boot @ version 1.0.2, and that was working fine.
It all started since the upgrade, and I made no code change.
SampleApplication.java
package org.sample;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value = "classpath:application.properties")
public class SampleApplication {
private static Logger logger = LoggerFactory
.getLogger(TaskManagerApplication.class);
@Value("${org.sample.sampleProperty}")
private static String sampleProperty;
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
System.out.print("SampleApplication started: " + sampleProperty);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
application.properties
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/mydb
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.driverClassName: com.mysql.jdbc.Driver
spring.jpa.show-sql: true
#Disable the ddl-auto:create once tables have been created
#spring.jpa.hibernate.ddl-auto: create
org.sample.sampleProperty=This is a sample property
photos.upload.dir=C:/temp/UserPhotos/
# Server port
server.port=8081
I have tried to add a PropertySourcesPlaceholderConfigurer bean and even PropertySourcesPlaceholderConfigurer but still the issue persists.
Anyone experienced this ? Or is there a new way to load the properties file ?
Please note that my db connection and server port are being read properly since my application can connect to db and I have to access it through the specified port. sampleProperty variable remains null though.
@Value
is not meant to work on static fieldsapplication.properties
are available automatically without specifying @PropertySource
for it.main()
method, you should do it after bean is constructed, for example by using @PostConstruct
Fully working example:
package demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Value("${org.sample.sampleProperty}")
private String sampleProperty;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct
public void postConstruct() {
System.out.print("SampleApplication started: " + sampleProperty);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With