Java – since migrating to spring boot 1.1 4. After release, @ value and application Properties problem
I have a problem because I moved to version 1.1 4. Spring launch of release
Variables annotated with @ value have not yet been filled with values, although they exist in application Properties Before that, I used spring boot @ version 1.0 2. And it works normally
It all started with the upgrade. I didn't change the code
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 tried to add a propertysourcesplaceholderconfigurer bean, or even propertysourcesplaceholderconfigurer, but there was still a problem
Has anyone experienced this? Or is there a new way to load property files?
Please note that my database connection and server port are reading correctly because my application can connect to the database and I must access it through the specified port The sampleproperty variable remains empty
Solution
>@ value is not applicable to static fields
Sufficient working examples:
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);
}
}
