Ho riscontrato un problema da quando mi sono spostato alla versione 1.1.4.RELEASE di Spring Boot.Problema con @Value e application.properties dal passaggio a Spring Boot 1.1.4.RELEASE
Le variabili che sono annotate con @Value non sono attualmente popolate con valori nonostante siano presenti in application.properties. Prima di questo stavo usando Spring Boot @ versione 1.0.2, e funzionava bene.
Tutto è iniziato dall'aggiornamento e non ho apportato alcuna modifica al codice.
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
ho cercato di aggiungere un fagiolo PropertySourcesPlaceholderConfigurer e anche PropertySourcesPlaceholderConfigurer ma ancora il problema persiste.
Chiunque abbia provato questo? O c'è un nuovo modo per caricare il file delle proprietà?
Si noti che la mia connessione db e la porta del server vengono lette correttamente poiché la mia applicazione può connettersi a db e devo accedervi tramite la porta specificata. La variabile sampleProperty rimane nulla.
Perché si dichiara una '@PropertySource '? questo dovrebbe funzionare out of the box (cioè senza '@ PropertySource' e il bean' PropertySourcesPlaceholderConfigurer' –
si lo so. Stavo usando senza '@ PropertySource' e' @ PropertySourcesPlaceholderConfigurer'. Li ho provati dopo aver riscontrato questo problema, ma per ( – Zaheeb
'@ Value' (come' @Autowired) non funziona su campi statici afaik.Se questo ha funzionato, su campi statici, direi che non era intenzionale. –