In diesem Artikel werden zwei Methoden der Konfigurationsdateien des Spring Reading Property -Konfiguration eingeführt. Schauen wir uns den spezifischen Inhalt an.
1. Durch die Frühlingsfabrik lesen
Beispiel:
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 -Schlüssel) {String propvalue = ""; if (cache.containsKey (key)) {propvalue = cache.get (key);} else {try {propvalue = beanfactory.resolveembeddedvalue ("$ {" + {" + ass {" + assex. "}"); cache.put (Schlüssel, propValue);} catch (illegalArgumentException ex) {ex.printstacktrace ();}} return propvalue;}}Feder XML -Konfiguration
<bean> <Eigenschaft name = "SystemPropertiesMoDeName" value = "System_Properties_Mode_Override"/> <Eigenschaft name = "ignoreresourcenotfound" value = "true"/> <Eigenschaft name = "Position"> <list> <Valchen> ClassPath: Props/$ {Property-Path}. </list> </property> </bean>Verwendung in einem Projekt
String mAXTimEnsecondSProp = PropertyConfig.getProperty ("MaxtimEnseconds");2. Verwenden Sie den Spirng -Programmcode direkt, um die Konfigurationsdateimethode des Projekts zu lesen
import org.springframework.core.io.classpatheResource; import org.springframework.core.io.resource; import org.springframework.core.io.support.Propertiesloadertile; public class test {/** * @param args */public static void main (String [] args) {String configfile = "d: /test/application.properties"; // Wenn sich die Konfigurationsdatei im Verzeichnis von classPaths befindet, können Sie das CLASSPATHRESOURCE -Objekt verwenden // Resource Resource = new classpathresource ("/application.properties"); Ressourcenressource = new FileSystemResource (configFile); try {properties property = propertiesloaderUtils.loadProperties (Ressource); String driver = property.getProperty ("jdbc.driver"); String url = property.getProperty ("jdbc.url"); String username = property.getProperty ("jdbc.username"); String password = Eigenschaft.getProperty ("jdbc.password"); } catch (ioException e1) {//log.Error("Read -Konfigurationsdatei fehlgeschlagen ", e1); }}}Wenn sich die Konfigurationsdatei im CLASSPATH -Verzeichnis befindet, können Sie das CLASSPATHRESource -Objekt verwenden
Ressourcenressource = new classpathresource ("/application.Properties");Zusammenfassen
Bei dem obigen dreht sich alles um das Lesen des Beispielcode der Eigenschaftskonfigurationsdatei mithilfe der Spring Factory. Ich hoffe, es wird für alle hilfreich sein. Interessierte Freunde können weiterhin auf andere verwandte Themen auf dieser Website verweisen. Wenn es Mängel gibt, hinterlassen Sie bitte eine Nachricht, um darauf hinzuweisen. Vielen Dank an Freunde für Ihre Unterstützung für diese Seite!