この記事では、スプリング読み取りプロパティ構成ファイルの2つの方法を紹介します。特定のコンテンツを見てみましょう。
1。春の工場を読んでください
例:
public class PropertyConfig {private static AbstractBeanFactory beanFactory = null;private static final Map<String,String> cache = new oncurrentHashMap<>();@Inject public PropertyConfig(AbstractBeanFactory beanFactory) {this.beanFactory = beanFactory;}/** * Get the Value of the configuration file based on the key * @param key * @return */public static string getProperty(string key){string propvalue = ""; if(cache.containskey(key)){propvalue = cache.get(key);} els {try {beanfactory.resolveembeddedValue( "$ {" $ {" + key.trim() +"} (IllegalArgumentException ex){ex.printstacktrace();}} return propvalue;}}}スプリングXML構成
<bean> <Property name = "SystemPropertiesModename" value = "system_properties_mode_override"/> <プロパティname = "ingoreresourcenotfound" value = "true"/> <プロパティ名= " </list> </property> </bean>
プロジェクトで使用します
string maxtimeinsecondsprop = propertyconfig.getProperty( "maxtimeinseconds");
2。spirngプログラムコードを直接使用して、プロジェクトの構成ファイルメソッドを読み取る
org.springframework.core.io.classpathresource; Import org.springframework.core.io.resource; Import org.springframework.core.io.io.support.propertiesloaderutils; Import org.springframework.core.io.filesemstemsemresource; public class test {/** * @param args */public static void main(string [] args){string configfile = "d:/test/application.properties"; //構成ファイルがclassPathディレクトリにある場合、classPathResourceオブジェクトを使用できます//リソースリソース= new ClassPathResource( "/Application.Properties");リソースリソース= new FilesystemResource(configfile); try {Properties Property = PropertiesLoaderUtils.LoadProperties(Resource); string driver = property.getProperty( "jdbc.driver"); string url = property.getProperty( "jdbc.url"); string username = property.getProperty( "jdbc.username");文字列password = property.getProperty( "jdbc.password"); } catch(ioexception e1){//log.error("Read Config File Failed "、e1); }}}構成ファイルがClassPathディレクトリにある場合、ClassPathResourceオブジェクトを使用できます
リソースリソース= new ClassPathResource( "/Application.Properties");
要約します
上記は、Spring Factoryを使用してプロパティ構成ファイルのサンプルコードを読み取ることです。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!