As we all know, spring can read the values in properties in a @value way, You only need to configure Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the configuration file
<BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Location"> <value>Classpath:config.properties</value> </ Property> </Bean>
So when you need to use these to get the value of the properties, you can use
@Value ("${sql.name}") private String sqlname;
But there is a problem, I declare a local variable every time I use the value in the config file. There is no way to directly read the values in the configuration file in code.
The answer is rewrite propertyplaceholderconfigurer.
Public classPropertyplaceholderextendsPropertyplaceholderconfigurer {Private StaticMap<string,string>PropertyMap; @Overrideprotected voidProcessproperties (configurablelistablebeanfactory beanfactorytoprocess, Properties props)throwsbeansexception {Super. Processproperties (beanfactorytoprocess, props); PropertyMap=NewHashmap<string, string>(); for(Object key:props.keySet ()) {String keystr=key.tostring (); String value=Props.getproperty (KEYSTR); Propertymap.put (keystr, value); } } //static method for accessing context properties Public StaticObject GetProperty (String name) {returnpropertymap.get (name); }}
In the configuration file, use the above class instead of Propertyplaceholderconfigurer
<BeanID= "Propertyconfigurer"class= "Com.gyoung.mybatis.util.PropertyPlaceholder"> < Propertyname= "Location"> <value>Classpath:config.properties</value> </ Property> </Bean>
This allows you to programmatically get the code directly
Propertyplaceholder.getproperty ("Sql.name");
Spring uses code to read the properties file