Das Spring-Boot-Boot von Micro-Framework war in letzter Zeit sehr beliebt. Der Autor hat auch den Trend verfolgt und nichts mehr gesagt. Jetzt werde ich ein Beispiel für das Lesen von Konfigurationsdateien geben.
Zunächst müssen Sie sich auf das folgende JAR -Paket in der POM -Datei verlassen
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </abhängig> </Abhängigkeiten>
Zweitens brauchen wir eine Frühlingsstart-Startkurs
@SpringbootApplication @enableConfigurationProperties ({propsconfig.class, ymlconfig.class}) öffentliche Klasse ReadApplication {public static void main (String [] args) {SpringApplication.run (readApplication.class, args); }}Das ist richtig, die propsconfig.class und ymlconfig.class wies in den Annotation @EnableConfigurationProperties darauf hin, dass es sich um Klassen handelt, die Props bzw. YML -Konfigurationsdateien lesen. Als nächstes werden wir die spezifische Implementierung von Leseeigenschaften bzw. YML -Konfigurationsdateien implementieren.
1. Read Properties -Konfigurationsdatei
Platzieren Sie eine Anwendung.
master.ds.driverClassName = com.mysql.jdbc.driver master.ds.url = jdbc: mysql: // localhost: 3306/test master.ds.username = root master.ds.ds.password = root master.ds.ds.ds.ds.ds.ds.ds.dds.dds.ds.dds.dds.in.initialSize = 1 1 1 1 Master master.ds.minidle = 10 Master.ds.TimeBetweenevictionRunsmillis = 60000 Master.ds.MinevictableIdletImemillis = 3000000 Master.ds.ValidationQuery = Select 'x' master.ds.Testilidle = true Master.destonbory = Falsch Master Master.ds.MaxopenPrepararedStatements = 100 Master.ds.removeabandoned = True Master.ds.RemoveaBandOnedTimeout = 1800 Master.ds.LogAboned = True
Das Lesen der Props -Konfigurationsklasse ist sehr einfach. Es ist im Grunde eine Pojo/VO -Klasse und laden Sie einfach die Annotation von @ConfigurationProperties in der Klasse.
@ConfigurationProperties (Präfix = "master.ds", spopations = "classPath: application.Properties") public class PropSconfig {private String driverClasName; private String -URL; privater String -Benutzername; privates Zeichenfolgenkennwort; private Stringfilter; private String maxactive; private String initialSize; private String Maxwait; public String getDriverClassName () {return triverClassName; } public void setDriverClassName (String -TreiberClassName) {this.DriverClassName = TRAVERCLASSNAME; } public String geturl () {return url; } public void seturl (String url) {this.url = url; } public String getUnername () {return userername; } public void setUnername (String -Benutzername) {this.username = userername; } public String getPassword () {Kennwort zurückgeben; } public void setPassword (Zeichenfolge) Passwort) {this.password = password; } public String getFilters () {Rückgabefilter; } public void setFilters (String -Filter) {this.Filters = filter; } public String getMaxactive () {return maxactive; } public void setMaxactive (String maxactive) {this.maxactive = maxactive; } public String getInitialSize () {return initialSize; } public void setInitialSize (String initialSize) {this.initialSize = InitialSize; } public String getMaxWait () {return maxwait; } public void setMaxwait (String maxwait) {this.maxwait = maxwait; }} Unit -Testklasse
@Runwith (SpringJunit4ClASSRunner.class) @springboottest (classes = readApplication.class) öffentliche Klasse ReadApplicationPropStests {@autowired private propsconfig propsconfig; @Test public void testDisplayPropsValue () {String driverClassName = propsconfig.getDriverClassName (); String url = propsconfig.geturl (); String userername = propsconfig.getUnername (); String password = propsconfig.getPassword (); String filters = propsconfig.getFilters (); String maxactive = propsconfig.getMaxactive (); String initialSize = propsconfig.getInitialSize (); String maxwait = propsconfig.getMaxwait (); System.out.println ("TRAVERCLASSNAME ->" + DRAVERCLASSNAME); System.out.println ("url ->" + url); System.out.println ("Benutzername ->" + Benutzername); System.out.println ("Passwort ->" + Passwort); System.out.println ("InitialSize ->" + InitialSize); System.out.println ("maxwait ->" + maxwait); }}Sie können die Ausgabe von Testinhalten in der Konsole sehen:
DriverClassName -> com.mysql.jdbc.driver URL -> JDBC: MySQL: // localhost: 3306/Test Benutzername -> Root -Passwort -> Root InitialSize -> 1 Maxwait -> 60000
2. Lesen Sie die YML -Konfigurationsdatei
Platzieren Sie eine Datei application.yml unter dem Klassenpfad mit dem allgemeinen Inhalt wie folgt:
MYPROPS: #CUSTOM -Eigenschaften und -Werte SimpleProp: SimplePropValue ArrayProps: 1,2,3,4,5 ListProp1: - Name: ABC Value: ABCVALUE - Name: EFG VALUE: EFGVALUE LISTPROP2: - CONFIG2VALUE1 - CONFIG2VAVLUE2 MAPPROPS: VALUEL1: VALUE12: Value2
Lesen Sie die Klasse der YML -Konfigurationsdatei.
@ConfigurationProperties (Präfix = "MYPROPS") // Eigenschaften unter MYPROPS in application.yml public class ymlconfig {private String SimpleProp; private String [] ArrayProps; private list <map <string, string >> listProp1 = new ArrayList <> (); // Attributwerte in prop1 privatlist <string> listProp2 = new ArrayList <> () erhalten; // Attributwerte in Prop2 private map <String, String> munterprops = new HashMap <> () erhalten; // Erhalten Sie den Attributwert in der Prop1 Public String getImpleProp () {return SimpleProp; } public void setSimpleProp (String SimpleProp) {this.SimpleProp = SimpleProp; } publiclist <map <String, String >> getListProp1 () {return listProp1; } publiclist <string> getListProp2 () {return listProp2; } public String [] getarrayProps () {return arrayProps; } public void setArrayProps (String [] ArrayProps) {this.ArrayProps = ArrayProps; } public map <String, String> getMapprops () {return martProps; } public void setMapprops (MAP <String, String> mungprops) {this.mapprops = mungprops; }} Unit -Testklasse
@Runwith (SpringJunit4ClASSRunner.class) @springBoottest (classes = readApplication.class) öffentliche Klasse ReadApplicationymltests {@autowired Private YMLConfig YMLConfig; @Test public void testDisplayymlValue () löst JsonProcessingException {System.out.println ("SimpleProp:" + ymlconfig.getSimpleProp ()) aus; ObjectMapper ObjectMapper = new ObjectMapper (); System.out.println ("ArrayProps:" + ObjectMapper.WriteValuEasString (ymlconfig.getArrayProps ())); System.out.println ("listProp1:" + ObjectMapper.WriteValuEasString (ymlconfig.getListProp1 ())); System.out.println ("listProp2:" + ObjectMapper.WriteValuEasString (ymlconfig.getListProp2 ())); System.out.println ("mapProps:" + ObjectMapper.WriteValuEasString (ymlconfig.getMapprops ())); }} Sie können die Ausgabe von Testinhalten in der Konsole sehen:
SimpleProp: SimplePropValue ArrayProps: ["1", "2", "3", "4", "5"] ListProp1: [{"Name": "ABC", "Wert": "Abcvalue"}, {"Name": "Efg", "Wert": "EfgValue"} "}] listProp22: ["config2Value1", "config2Vavlue2"] mapProps: {"key1": "value1", "key2": "value2"} Ist es nicht unglaublich? Ohne Spring's ApplicationContext.xml -Datei kann es reibungslos ausgeführt werden.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.