Come posso iniettare valori in una mappa dal file delle proprietà usando l’annotazione @Value in spring?
La mia class Spring Spring è e ho provato ad usare $ ma ho ricevuto il seguente messaggio di errore
Imansible eseguire il campo autowire: private java.util.Map Test.standard; l’eccezione nidificata è java.lang.IllegalArgumentException: Imansible risolvere il segnaposto “com.test.standard” nel valore stringa “$ {com.test.standard}”
@ConfigurationProperty("com.hello.foo") public class Test { @Value("${com.test.standard}") private Map standard = new LinkedHashMap private String enabled; }
Ho le seguenti proprietà in un file .properties
com.test.standard.name1=Pattern1 com.test.standard.name2=Pattern2 com.test.standard.name3=Pattern3 com.hello.foo.enabled=true
Credo che Spring Boot supporti il caricamento immediato delle mappe delle proprietà con l’ annotazione @ConfigurationProperties .
Secondo i documenti puoi caricare le proprietà:
my.servers[0]=dev.bar.com my.servers[1]=foo.bar.com
in bean come questo:
@ConfigurationProperties(prefix="my") public class Config { private List servers = new ArrayList (); public List getServers() { return this.servers; } }
Ho usato la funzione @ConfigurationProperties prima, ma senza caricare nella mappa. È necessario utilizzare l’annotazione @EnableConfigurationProperties per abilitare questa funzione.
Le novità su questa funzione sono la possibilità di convalidare le proprietà .
È ansible iniettare valori in una mappa dal file delle proprietà utilizzando l’annotazione @Value
come questa.
La proprietà nel file delle proprietà.
propertyname={key1:'value1',key2:'value2',....}
Nel tuo codice
@Value("#{${propertyname}}") private Map propertyname;
Nota l’hashtag come parte dell’annotazione.
È ansible .properties
come una mappa nella class utilizzando @Resource
annotazione @Resource
.
Se stai lavorando con la XML based configuration
, quindi aggiungi sotto bean nel tuo file di configurazione di spring:
Per, annotazione basata:
@Bean(name = "myProperties") public static PropertiesFactoryBean mapper() { PropertiesFactoryBean bean = new PropertiesFactoryBean(); bean.setLocation(new ClassPathResource( "your.properties")); return bean; }
Quindi puoi ritirarli nella tua applicazione come una mappa:
@Resource(name = "myProperties") private Map myProperties;
Ecco come l’abbiamo fatto. Due classi di esempio come segue:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.annotation.EnableKafka; @EnableKafka @Configuration @EnableConfigurationProperties(KafkaConsumerProperties.class) public class KafkaContainerConfig { @Autowired protected KafkaConsumerProperties kafkaConsumerProperties; @Bean public ConsumerFactory consumerFactory() { return new DefaultKafkaConsumerFactory<>(kafkaConsumerProperties.getKafkaConsumerConfig()); } ... @Configuration @ConfigurationProperties public class KafkaConsumerProperties { protected Map kafkaConsumerConfig = new HashMap<>(); @ConfigurationProperties("kafkaConsumerConfig") public Map getKafkaConsumerConfig() { return (kafkaConsumerConfig); } ...
Per fornire la configurazione di kafkaConsumer da un file delle proprietà, puoi utilizzare: mapname [chiave] = valore
//application.properties kafkaConsumerConfig[bootstrap.servers]=localhost:9092, localhost:9093, localhost:9094 kafkaConsumerConfig[group.id]=test-consumer-group-local kafkaConsumerConfig[value.deserializer]=org.apache.kafka.common.serialization.StringDeserializer kafkaConsumerConfig[key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
Per fornire la configurazione di kafkaConsumer da un file yaml, puoi usare “[chiave]”: valore nel file application.yml:
kafkaConsumerConfig: "[bootstrap.servers]": localhost:9092, localhost:9093, localhost:9094 "[group.id]": test-consumer-group-local "[value.deserializer]": org.apache.kafka.common.serialization.StringDeserializer "[key.deserializer]": org.apache.kafka.common.serialization.StringDeserializer