Bevor Sie diesen Artikel lesen, können Sie sich zunächst auf den Artikel " Verständnis der Abhängigkeitsinjektion und Kontrolle der Inversion im Frühling " beziehen, um den relevanten Inhalt der Abhängigkeitsinjektion und der Steuerinversion zu erfahren.
Drei Arten der Abhängigkeitsinjektion
Eigenschaftsinjektion, injizieren Sie den Eigenschaftswert der Bean über die Settermethode oder die Injektion des abhängigen Objektkonstrukts (Injektionskonstruktion) (selten verwendet)
Beispiel
Hier verwenden wir Spring-4.3.2, Maven-Konfigurationsdatei
<dependency> <groupid>org.springframework</groupid> spring-core</artifactid> <version>${org.springframework-version}</version> <exclusions> <exclusion> <groupid>commons-logging</groupid> commons-logging</artifactid> </exclusion> </exclusions></dependency><dependency> <gruppe> org.springFramework </Groupid> Spring-Beans </artifactID> <version> $ {org.springFramework-Version} </Version> </abhängig> <Depopentcy> <gruppe> org.springFramework </Groupid> Spring-Aop </artifactid> <version>${org.springframework-version}</version></dependency><dependency> <groupid>org.springframework</groupid> spring-context</artifactid> <version>${org.springframework-version}</version></dependency><dependency> <groupid>commons-logging</groupid> Commons-Logging </artifactId> <version> 1.2 </Version> </abhängig> <!-junit-> <abhängigkeit> <GroupID> junit </gruppen> junit </artifactid> <version> 3.8.1 </Version> <SchopsapplicationContext.xml Konfigurationsdatei
<!-? xml Version = "1.0" coding = "utf-8"?-> <beans xmlns = "https://www.springframework.org/schema/beans" xmlns: p = "https xmlns: util = "https://www.springframework.org/schema/util" xmlns: xsi = "https://www.w3.org/2001/xmlschema-instance" xsi: Schemalocation = "https https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/util https://www.springframework.org/schema/util/util/util/util/util/spring: Klasse: Der vollständige Klassenname der Bean im Container und erstellen Sie eine Bean im IOC-Container durch Reflexion. Die Bean-Klasse muss also einen Parameterkonstruktor haben-> <bean id = "helloWorld"> <Eigenschaftsname = "Name" value = "Crystal"> </Eigenschaft> </bean> <! <bean id = "car"> <constructor-arg Index = "0" value = "benchi"> </constructor-arg> <constructor-arg Index = "1" type = "double" value = "200000.0"> </constructor-Arg> </bean> <bean id = "car1". Die Wertkenntnisse-> <constructor-arg type = "java.lang.String"> <value> <! value = "crystal"> </property> <Eigenschaft name = "Alter" value = "20"> </property> <!-Sie können Ref verwenden, um Beziehungen zwischen Referenzen zu erstellen. value = "changanfute"> </constructor-arg> <constructor-arg value = "3000000"> </constructor-arg> <constructor-arg value = "240"> </constructor-arg> </bean> </Eigenschaft> <! Ref = "CAR1"> </property> <!- Zuweisen Werte den Kaskadierungseigenschaften. HINWEIS: Eigenschaften müssen initialisiert werden, bevor ihnen die Kaskadierungseigenschaften Werte zugewiesen werden. Sie unterscheiden sich von structs2-> <Eigenschaft name = "car.price" value = "400000"> </property> </bean> <!-Testerkonfigurationssammlungseigenschaften-> <bean id = "person3"> <Eigenschaft name = "Name" value = "Barry"> </Property> <älter "value =" car ". bean="car1"> <bean> <constructor-arg value="changanFute"></constructor-arg> <constructor-arg value="3000000"></constructor-arg> <constructor-arg value="240"></constructor-arg> </bean> </ref></ref></list> </property> </bean> <!-- Configure the property Wert der Karte-> <bean id = "newperson"> <Eigenschaft name = "name" value = "lina"> </property> <Eigenschaft name = "Alter" value = "22"> </Eigenschaft> <Eigenschaft name = "cars"> <!-Verwenden Sie den Kartenknoten und Map-Eintrag Untertreffer, um Mitgliedsvariablen des Kartens zu konfigurieren. value-ref = "car1"> </Eintrag> </map> </property> </bean> <!-Eigenschaften Eigenschaften-Eigenschaftswert-> <bean id = "dataSource"> <Eigenschaft name = "Eigenschaften"> <!-Verwenden Sie Props und Props-Props, um Werte den Eigenschaften zuzuweisen. key = "jdbcurl"> jdbc: mysql: // test </prop> <prop key = "jdbcdriver"> com.mysql.jdbc.driver </prop> </props> </property> </bean> <!- Konfigurieren Sie eine Singleton-Sammlungsbean für Referenzen nach mehreren Beans. Sie müssen den Util-Namespace importieren-> <util: list id = "cars"> <ref bean = "car"> <ref bean = "car1"> </ref> </ref> </util: list> <bean id = "person4"> <Eigenschaft name = "name" value = "Jackie"> </Property> <Eigenschaftsname ". </bean> <!-Zuweisen Werte den Beaneigenschaften über den P-Namespace Werte zuerst importieren Sie den P-Namespace zuerst.
1. Nachstehend finden Sie einfache Attributinjektions- und Konstrukt -Injektionstestklassen
Car.java
Paket com.spring.test; public class Car {private String name; privat int maxspeed; {this.name = name; this.price = price; this.maxSpeed = maxspeed;} public void setPrice (Doppelpreis) {this.price = price;}@overside public String toString () {return "car [name:" + name + "," + preis + ", maxspeed:" + maxspeed + ";HelloWorld.java
Paket com.spring.test; public class helloWorld {private String name; public helloWorld () {System.out.println ("HelloWorld Constructor ...");} public String getName () {return name;} public void setname (String name) {system.outln ("setname:" + name); toString () {return "hello", + name;}}Person.java
Paket com.spring.test; public class Person {private String name; privates int age; public Car Car; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getage () {return ay;} Public void void setage (Int Age) {this.age -aga -aga astcar astcar astcar astcar; oderMain.java
paket com.spring.test; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework HelloWorld (); hello.setName ("barry"); Beans beim Initialisieren des Kontextes */applicationContext context = new classPathXMLApplicationContext ("applicationContext.xml"); // HelloWorld hello1 = (helloWorld) context.getbean ("helloWorld"); // das Bean -Objekt über ID helloWorld hello1 = context.getBean (helloWorld.class); // das Bean -Objekt nach Typ erhalten (erfordert, dass es nur ein Objekt dieses Typs im ioc container) system.out.println (Hallo) geben kann; ClassPathXmlApplicationContext ("applicationContext.xml"); Car Car = (Car) context.getBean ("Car"); // das Bean -Objekt nach Typ (erfordert, dass es nur ein Objekt dieses Typs im IOC -Container geben kann. (Person) context.getbean ("Person"); System.out.println (Person);}} 2. Unten finden Sie die Testklasse für die Sammlung
Newperson.java
paket com.spring.test.collections; import java.util.map; import com.spring.test.car; public class newperson {private String name; private int age; private map <String, car = "> cars; public String getName () {return name; setage (int ay) {this.age = age;} public map <String, car = ""> getCars () {return cars;} public void setCars (Karte <String, car = ""> cars) {this.cars = cars;}@override public String toString () {return "person: [name =" + ", ay" ay "ay" ay "ay" ay "ay" "ay" ’’ "", "’ ’" "," ’’ "", "", "", "", "", "", "", "", "", "", "" "," "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",Person.java
paket com.spring.test.collections; import Java.util.List; import com.spring.test.car; public class Person {private String name; private int age; publiclist <Car> cars; public String getName () {return name;} public void setname (String name) {this. {this.age = age;} publiclist <Car> getCars () {return cars;} public void setCars (List <Car> Cars) {this.cars = cars;}@overside public String toString () {return "Person: [name =" + name + ", älter =" + älter + ", cars =" + cars + "]"}}}}}}}}}}}}}}}}}}}}DataSource.java
package com.spring.test.collections;import java.util.Properties;public class DataSource {private Properties properties;public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Override public String toString() {return "DataSource: [Eigenschaften: " + Eigenschaften +"] ";}}Main.java
Paket com.spring.test.collections; import org.junit.test; import org.springframework.context.ApplicationContext; ClasspathxmlApplicationContext ("applicationContext.xml"); Person Person = (Person) context.getBean ("Person3"); System.out.println (Person); newperson newperson = (newperson) context.getbean ("newperson"); system.println (newperson); context.getbean ("dataSource");Zusammenfassen
In diesem Artikel geht es um das Beispiel für das Beispiel für die Injektionsmethode für Spring Framework -Abhängigkeit. Ich hoffe, es wird für alle hilfreich sein. 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!