قبل قراءة هذه المقالة ، يمكنك أولاً الرجوع إلى مقالة " فهم حقن التبعية والتحكم في انعكاس في الربيع " للتعرف على المحتوى ذي الصلة لحقن التبعية وانعكاس التحكم.
ثلاث طرق لحقن التبعية
حقن الممتلكات ، حقن قيمة خاصية الفول من خلال طريقة Setter أو حقن طريقة حقن كائن تابع (نادرًا ما تستخدم)
مثال
هنا نستخدم ملف Spring-4.3.2 ، ملف تكوين Maven
<Rependency> <roupEd> org.springframework </rougiD> spring-core </stifactid> <soph> $ {org.springframework-version} </version> <visplusions> <visplusion> <roupaD> commons-logging </groupid-logging </artifactId> <Groper> org.springframework </rougiD> beans Spring-beans </artifactId> <soph> $ {org.springframework-version} </version> </sependency> <redenced> <roupiD> org.springframework </rougiD> spring-aop </restifactid> <soph> $ {org.springframework-version} </version> </sependency> <reperence> <roupiD> org.springframework </rougeid> spring-context </sempactid> <sophid> $ {org.springframework-version} </version> commons-logging </artifactId> <الإصدار> 1.2 </version> </repreadency> <!-Junit-> <reperency> <roupiD> junit </rougiD> Junit </stifactid> <الإصدار> 3.8.1 </الإصدار>ApplicationContext.xml ملف التكوين
<!-؟ xmlns: util = "https://www.springframework.org/schema/Util" xmlns: xsi = "https://www.w3.org/2001/xmlschema- XSI: schemalocation = "https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https https://www.springframework.org/schema/util/spring-util.xsd "> <!-تكوين معرف الفول: حدد فئة كائن الفول: اسم الفاصوليا الكامل للحبة في الحاوية ، وإنشاء فول في حاوية IOC من خلال الانعكاس ، لذلك يجب أن يكون فئة الفول بنية للممتلكات-> < value = "crystal"> </property> </bean> <!-يتيح لك تكوين الفاصوليا من خلال طرق المنشئ تحديد موقع ونوع المعلمات لتمييز المُنشئين الزائد-> <bean id = "car"> <constructor-arg index = "0" value = "benchi"> </bustructor-arg> value = "200000.0"> </constructor-arg> </bean> <bean id = "car1"> <!-إذا كان الحرفي يحتوي على أحرف خاصة ، فاستخدم <! </constructor-arg> <constructor-arg index = "1" type = "int" value = "200"> </constructor-arg> </bean> <bean id = "person"> <property name = "name" value = "crystal"> </property name = "Age" value =! ref = "car"> </sopperation>-> <!-<property name = "car"> <ref bean = "car2"/> </spregence>-> <!-<property name = "car"> <ban> <constructor-arg value = "changanfute value = "240"> </constructor-arg> </bean> </swerperation>-> <!-اختبار تعيين NULL-> <!-<property name = "car"> <null/> </property>-> <property name = "car" ref = "//property> <! ملاحظة: يجب تهيئة الخصائص قبل أن يتم تعيين قيم لخصائص متتالية. فهي مختلفة عن structs2-> <property name = "car.price" value = "400000"> </property> </bean> <!-اختبار خصائص مجموعة تكوين التكوين-> <bean id = "person3"> <property name = "name" value = "barry"> </propert bean = "car1"> <bean> <bustructor-arg value = "changanfute"> </constructor-arg> <constructor-arg value = "3000000"> </constructor-arg> </arg constructor-arg value = "240"> MAP-> <bean id = "newPerson"> <property name = "name" value = "lina"> </sprement> <property name = "age" value = "22" value-ref = "car1"> </prower> </map> </property> </boan> <!-تكوين قيمة خصائص الخصائص-> <bean id = "dataSource"> <property name = "properties"> <!-استخدم الدعائم و Prop to endract to properties-> key = "jdbcurl"> jdbc: mysql: // test </prop> <prop key = "jdbcdriver"> com.mysql.jdbc.driver </prop> </props> </property> </bean> <!- تكوين حبة مجموعة singleton للمرجع عن طريق الفاصوليا المتعددة. تحتاج إلى استيراد مساحة الاسم UTIL-> <UTIL: LIST ID = "CARS"> <ref bean = "car"> <ref bean = "car1"> </ref> </ref> </util: list> <bean id = "person4"> <property name = "name" value = "jackie"> </propert ref = "cars"> </splyment> </boan> <!-تعيين قيم لخصائص الفاصوليا من خلال مساحة الاسم P ، تحتاج إلى استيراد مساحة الاسم P أولاً ، وهو أكثر إيجازًا من أساليب التكوين التقليدية-> <bean id = "person5" p: age = "32" p: cars-ref = "p: name = queue"
1. أدناه هي حقن السمة البسيطة وبناء فئات اختبار الحقن
car.java
package com.spring.test ؛ class class car {private string name ؛ private int maxspeed ؛ price double price ؛ public car () {} السيارة العامة (اسم السلسلة ، السعر المزدوج) {this.name = name ؛ this.price = price ؛ {this.name = name ؛ this.price = price ؛ this.maxspeed = maxspeed ؛} public void setPrice (price double) {this.price = price ؛}@Override public String ToString () {return "Car [name:Helloworld.java
package com.spring.test ؛ public class helloWorld {private string name ؛ public HelloWorld () {system.out.println ("HelloWorld Constructor ...") ؛} السلسلة العامة getName () {return name ؛ tostring () {return "hello ،" + name ؛}}شخص
package com.spring.test ؛ public class person {private string name ؛ private int age ؛ public car ؛ public string getName () {return name ؛} public void setName (string name) {this.name = name ؛ {this.car = car ؛}@تجاوز السلسلة العامة tostring () {return "person: [name =" + name + "، Age =" + Age + "، Car =" + Car + "] ؛}}main.java
حزمة com.spring.test ؛ استيراد org.junit.test ؛ استيراد org.springframework.context.applicationContext ؛ استيراد org.springframewework.context.support.classpathxmlabplicationContext HelloWorld () ؛ hello.setname ("barry") ؛ system.out.println ("print:" + hello + "/n") ؛ الفاصوليا عند تهيئة السياق */applicationContext = جديد classpathxmlabplicationContext ("ApplicationContext.xml") ؛ // helloworld hello1 = (helloworld) context.getBean ("helloworld") ؛ // احصل على كائن الفول من خلال معرف HelloWorld Hello1 = context.getBean (helloWorld.class) ؛ // احصل على كائن الفول من النوع (يتطلب أنه لا يمكن أن يكون هناك سوى كائن واحد من هذا النوع في حاوية IOC) system.out.println (hello1) ؛} classpathxmlapplicationContext ("applicationContext.xml") ؛ Car Car = (car) context.getBean ("Car") ؛ // احصل على كائن الفول بواسطة النوع (يتطلب أن يكون هناك كائن واحد فقط من هذا النوع في حاوية IOC) (شخص) سياق. 2. أدناه فئة الاختبار للمجموعة
newperson.java
package com.spring.test.collections ؛ استيراد java.util.map ؛ استيراد com.spring.test.car ؛ الفئة العامة newPerson {private string name ؛ int ation ؛ private map <string ، car = "> cars ؛ public string getName () {return name ؛ setage (int age) {this.age = age ؛} الخريطة العامة <string ، car = ""> getCars () {return cars ؛} setCars public void (map <string ، car = ""> cars) {this.cars = cars ؛}@Outdride public tostring () {return "person: "]" ؛}}شخص
package com.spring.test.collections ؛ import java.util.list ؛ استيراد com.spring.test.car ؛ public class person {private string name ؛ private int end ؛ public list <car> cars ؛ public string getName () {int end end end end end {this.age = age ؛} قائمة عامة <car> getCars () {return cars ؛} setCars public void (قائمة <car> cars) {this.cars = cars ؛}@@string public tostring () {return "person:DataSource.java
package com.spring.test.collections ؛ استيراد java.util.properties ؛ الطبقة العامة dataSource {خصائص الخصائص الخاصة ؛ الخصائص العامة getProperties () {properties ؛} [الخصائص: " + خصائص +"] "؛}}main.java
package com.spring.test.collections ؛ استيراد org.junit.test ؛ استيراد org.springframework.context.applicationContext ؛ استيراد org.springframework.context.support.classpathxmlication ؛ classpathxmlapplicationContext ("ApplicationContext.xml") ؛ شخص شخص = (شخص) سياق. context.getBean ("datasource") ؛ system.out.println (dataSource) ؛ person person4 = (person) context.getBean ("person4") ؛ system.out.println (person4) ؛ person5 = (person) context.getBean ("person5") ؛ system.out.printlnلخص
ما سبق هو كل شيء عن مثال طريقة حقن التبعية في إطار الربيع في هذه المقالة ، آمل أن يكون ذلك مفيدًا للجميع. إذا كانت هناك أي أوجه قصور ، فيرجى ترك رسالة لإشارةها. شكرا لك يا أصدقائك لدعمكم لهذا الموقع!