ก่อนที่จะอ่านบทความนี้คุณสามารถอ้างถึงบทความ " การทำความเข้าใจการฉีดพึ่งพาการพึ่งพาและการผกผันในฤดูใบไม้ผลิ " เพื่อเรียนรู้เกี่ยวกับเนื้อหาที่เกี่ยวข้องของการฉีดพึ่งพาและการผกผันควบคุม
สามวิธีในการฉีดพึ่งพา
การฉีดสมบัติฉีดค่าคุณสมบัติของถั่วผ่านวิธีการตั้งค่าหรือการสร้างวัตถุที่ขึ้นอยู่กับการสร้างโรงงานผลิตวิธีการฉีด (ไม่ค่อยใช้)
ตัวอย่าง
ที่นี่เราใช้ Spring-4.3.2, ไฟล์การกำหนดค่า Maven
<การพึ่งพา> <roupId> org.springframework </groupId> Spring-Core </artifactid> <sersion> $ {org.springframework-version} </เวอร์ชัน> <exclusions> <exclusion> <RoupID> org.springframework </groupId> Spring-Beans </artifactId> <sersion> $ {org.springframework-version} </senture> </predency> <การพึ่งพา <cersion> $ {org.springframework-version} </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupId> org.springframework </groupId> สปริงเท็กซ์ </artifactid> <version> $ {org.springframework-version Commons-Logging </artifactId> <cersion> 1.2 </เวอร์ชัน> </predency> <!-junit-> <pendency> <sderctelไฟล์กำหนดค่า ApplicationContext.xml
<!-? xml version = "1.0" การเข้ารหัส = "utf-8"?-> <ถั่ว xmlns = "https://www.springframework.org/schema/beans" xmlns: p = "https://ww.springframework xmlns: util = "https://www.springframework.org/schema/util" xmlns: xsi = "https://www.w3.org/2001/xmlschema-instance https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/util https://www.springframework.org/schema/STILTIL/SCHEMATIL/SCHEMATIL/SCHEMATILTILTILTILTIL.OD คลาสวัตถุถั่ว: ชื่อคลาสเต็มของถั่วในภาชนะและสร้างถั่วในคอนเทนเนอร์ IOC ผ่านการสะท้อนดังนั้นคลาสถั่วจะต้องมีตัวสร้างพารามิเตอร์-> <bean id = "helloWorld"> <property name = "name" value = "crystal"> constructors-> <bean id = "car"> <constructor-arg index = "0" value = "benchi"> </constructor-arg> <constructor-Arg index = "1" type = "double" value = "200000.0"> ห่อค่าแอตทริบิวต์ด้วยโหนดเด็กที่มีค่า-> <constructor-arg type = "java.lang.string"> <ค่า> <!-[cdata [<hanghai->]> </value> </constructor-arg> <constructor-Arg arg = "1" type = "int" value = " name = "name" value = "crystal"> </property> <property name = "age" value = "20"> </property> <!-คุณสามารถใช้ REF เพื่อสร้างความสัมพันธ์ระหว่างการอ้างอิง-> <!-<property name = "car" ref = "car"> </property>-> <! <constructor-arg value = "Changanfute"> </constructor-Arg> <constructor-Arg value = "3000000"> </constructor-Arg> <constructor-Arg value = "240"> </constructor-Arg> </epovery> name = "car" ref = "car1"> </property> <!- กำหนดค่าให้กับคุณสมบัติ cascading หมายเหตุ: คุณสมบัติจะต้องเริ่มต้นก่อนที่จะสามารถกำหนดค่าให้กับคุณสมบัติการเรียงซ้อน พวกเขาแตกต่างจาก structs2-> <property name = "car.price" value = "400000"> </property> </epean> <!-ทดสอบคุณสมบัติการรวบรวมการกำหนดค่า-> <bean id = "person3"> <property name = "name" value = "Barry"> Bean = "car1"> <bean> <constructor-arg value = "changanfute"> </constructor-Arg> <constructor-Arg value = "3000000"> </constructor-Arg> <constructor-Arg value = "240"> ค่าของแผนที่-> <bean id = "newPerson"> <property name = "name" value = "lina"> </property> <property name = "age" value = "22"> </property> <property name = "Cars"> <! value-ref = "car1"> </sentry> </map> </property> </ebean> <!-กำหนดค่าคุณสมบัติคุณสมบัติ-> <bean id = "DataSource"> <property name = "คุณสมบัติ"> <! key = "jdbcurl"> jdbc: mysql: // ทดสอบ </prop> <prop key = "jdbcdriver"> com.mysql.jdbc.driver </prop> </props> </porement> คุณต้องนำเข้า util namespace-> <util: list id = "cars"> <ref bean = "car"> <ref bean = "car1"> </ref> </ref> </util: รายการ> <bean id = "person4"> <property name = "name" value = "Jackie"> ref = "cars"> </proty> </ebean> <!-การกำหนดค่าให้กับคุณสมบัติถั่วผ่าน p namespace คุณต้องนำเข้า p namespace ก่อนซึ่งมีความรัดกุมมากกว่าวิธีการกำหนดค่าแบบดั้งเดิม-> <bean id = "person5" p: age = "32" P: รถยนต์
1. ด้านล่างเป็นการฉีดแอตทริบิวต์อย่างง่ายและสร้างคลาสทดสอบการฉีด
car.java
แพ็คเกจ com.spring.test; รถคลาสสาธารณะ {ชื่อสตริงส่วนตัว; int ส่วนตัว maxspeed; ราคาสองเท่าส่วนตัว, รถสาธารณะ () {} รถสาธารณะ (ชื่อสตริง, ราคาคู่) {this.name = name; this.price = ราคา;} รถสาธารณะ {this.name = name; this.price = price; this.maxspeed = maxspeed;} โมฆะสาธารณะ setPrice (ราคาสองเท่า) {this.price = price;}@@reverride สตริงสาธารณะ toString () {return "รถ [ชื่อ:" ชื่อ + ", ราคา:" + ราคา + ", maxspeed:helloworld.java
แพ็คเกจ com.spring.test; คลาสสาธารณะ helloWorld {ชื่อสตริงส่วนตัว; public helloWorld () {system.out.println ("helloWorld constructor ... ");} สตริงสาธารณะ getName () {ชื่อกลับ; toString () {return "hello," + name;}}person.java
แพ็คเกจ com.spring.test; บุคคลระดับสาธารณะ {ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว, รถยนต์สาธารณะ, สตริงสาธารณะ getName () {ชื่อคืน;} โมฆะสาธารณะ setname (ชื่อสตริง) {this.name = name;} public int getage () {return age; {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.springframework.context.support.classpathxmlapplicationcontext; คลาสสาธารณะ helloWorld (); hello.setName ("Barry"); System.out.println ("Print:" + Hello + "/N"); // โหลดไฟล์การกำหนดค่าสปริง/*** ไฟล์การกำหนดค่าสปริงโหลด* ApplicationContext เป็น IOC */applicationContext context = ใหม่ classPathxMlApplicationContext ("ApplicationContext.xml"); // helloWorld hello1 = (helloWorld) บริบท. getBean ("helloWorld"); // รับวัตถุถั่วผ่าน id helloWorld hello1 = context.getBean (helloWorld.class); // รับวัตถุถั่วตามประเภท (ต้องการให้มีเพียงวัตถุเดียวของประเภทนี้ในคอนเทนเนอร์ IOC) system.out.println (hello1);}@test testcontructor = (Car) Context.getBean ("Car"); // รับวัตถุถั่วตามประเภท (ต้องการให้มีเพียงวัตถุเดียวของประเภทนี้ในคอนเทนเนอร์ IOC) CAR car1 = (Car) บริบท GetBean ("Car1"); system.out.println (car); system.out.println (car1); context.getBean ("person"); system.out.println (บุคคล);}} 2. ด้านล่างเป็นคลาสทดสอบสำหรับคอลเลกชัน
newperson.java
แพ็คเกจ com.spring.test.collections; นำเข้า java.util.map; นำเข้า com.spring.test.car; คลาสสาธารณะ newperson {ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว; แผนที่ส่วนตัว <สตริง, car = "> รถยนต์ setage (อายุ int) {this.age = age;} แผนที่สาธารณะ <String, car = "> getCars () {return cars;} public void setcars (แผนที่ <String, car =" "> cars) {this.cars = cars;}@override String String toString () {return"person.java
แพ็คเกจ com.spring.test.collections; นำเข้า java.util.list; นำเข้า com.spring.test.car; บุคคลระดับบุคคล {ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว; รายการสาธารณะ <sar> รถยนต์, สตริงสาธารณะ getName () {ชื่อคืน;} {this.age = age;} รายการสาธารณะ <bar> getCars () {return cars;} public void setcars (รายการ <ar> cars) {this.cars = cars;}@แทนที่สตริงสาธารณะ toString () {return "บุคคล: [name =" + name + ", อายุ ="dataSource.java
แพ็คเกจ com.spring.test.collections; นำเข้า java.util.properties; DataSource คลาสสาธารณะ {คุณสมบัติส่วนตัวคุณสมบัติ, คุณสมบัติสาธารณะ getProperties () {คุณสมบัติคืน;} void setProperties (Properties) [คุณสมบัติ: " + คุณสมบัติ +"] ";}}Main.java
แพ็คเกจ com.spring.test.collections; นำเข้า org.junit.test; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext; classpathxmlapplicationContext ("ApplicationContext.xml"); บุคคลบุคคล = (บุคคล) บริบท. getBean ("person3"); system.out.println (บุคคล); newperson newperson = (newperson) บริบท. getBean ("newperson"); system.out.println Context.getBean ("DataSource"); System.out.println (DataSource); Person Person4 = (บุคคล) บริบท. getBean ("person4"); system.out.println (person4); person person5 = (บุคคล) บริบท. getBean ("person5"); system.out.printlnสรุป
ข้างต้นเป็นตัวอย่างของวิธีการฉีดพึ่งพากรอบฤดูใบไม้ผลิในบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น ขอบคุณเพื่อนที่ให้การสนับสนุนเว็บไซต์นี้!