Resources/config/config-dev.properties
Es.ip.node=xxxxxxx
Cluster.name=xxxxxxx
Client.transport.sniff=xxxxxxx
Load Properties
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Ignoreresourcenotfound" value= "true"/>
<property name= "Locations" >
<list>
<value>classpath:config/config-${env}.properties</value>
</list>
</property>
</bean>
The bean that corresponds to the attribute
Import Org.springframework.beans.factory.annotation.Value;
Import org.springframework.stereotype.Component;
/**
* <p>description: Read properties file </p>
*
* @Author: Austin
**/
@Component
public class Esparams {
Public StaticString Esipnode;
Public StaticString clustername;
Public StaticString Clienttransportsniff;
@Value ("${es.ip.node}")
public void Setesipnode (String esipnode) {
Esparams.esipnode = Esipnode;
}
@Value ("${cluster.name}")
public void Setclustername (String clustername) {
Esparams.clustername = clustername;
}
@Value ("${client.transport.sniff}")
public void Setclienttransportsniff (String clienttransportsniff) {
Esparams.clienttransportsniff = Clienttransportsniff;
}
}
Note: The color of the place, a little bit less error or write wrong, in addition to the automatic setter method to remove the modifier static , otherwise spring cannot inject
You can now use esparams.xxx directly in any class for easy reference, such as Esparams.esipnode.
Spring reads a configuration file, injecting values into a static field