1. Injeksi Ketergantungan (DI)
Injeksi ketergantungan kedengarannya sangat mendalam, tetapi sebenarnya: menetapkan nilai ke atribut. Ada dua metode secara total. Yang pertama adalah dalam bentuk parameter konstruktor, dan yang lainnya dalam bentuk metode pengaturan.
1 injeksi konstruktor
1 suntikan menggunakan konstruktor
Metode injeksi menggunakan XML
A. Urutan parameter
<constructor-arg index = "0"> <value> zhang san </ value> </constructor-Arg>
<constructor-arg index = "1"> <value> 56 </ value> </constructor-Arg>
B. Jenis parameter
<constructor-arg type = "java.lang.integer"> <value> 56 </value> </constructor-Arg>
<constructor-arg type = "java.lang.string"> <value> zhang san </ value> </constructor-Arg>
Contoh spesifik
Jika Anda ingin menyuntikkan parameter ke kelas orang sekarang, siswa adalah kelas lain.
orang kelas publik {private string pid; nama string pribadi; Siswa Pribadi; orang publik (string pid, siswa siswa) {this.pid = pid; this.student = siswa; } orang publik (string pid, nama string) {this.pid = pid; this.name = name; }}Konfigurasikan ApplicationContext.xml. Jika tidak ada konfigurasi parameter yang dilakukan, kesalahan akan dilaporkan dan konstruktor yang sesuai tidak dapat ditemukan. Jika parameter yang sesuai dikonfigurasi, konstruktor yang sesuai harus dinyatakan di kelas.
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id = "orang"> <! Konstruktor dalam kelas orang adalah tanda sudut parameter. Nilai tipe parameter. If it is a basic property, use this assignment ref to refer to the type assign --> <constructor-arg index="0" type="java.lang.String" value="aaa"></constructor-arg> <constructor-arg index="1" ref="student"></constructor-arg> </bean> <bean id="person1"> <property name="pid" value="1"></property> </bean> <bean id = "student"> </bean> </bean>
Tulis kelas uji DixmlConstructortest dan lakukan debugging breakpoint. Anda akan menemukan bahwa sesuai dengan parameter konfigurasi, konstruktor yang dimasukkan adalah orang (string pid, siswa siswa)
kelas publik DixmlConstructortest {@test public void test1 () {applicationContext context = new ClassPathXMLapPlicationContext ("ApplicationContext.xml"); Orang orang = (orang) context.getBean ("orang"); }}2 Injeksi Menggunakan Metode Setter Properti
Metode injeksi menggunakan XML:
A. Suntikan kacang sederhana kacang sederhana termasuk dua jenis: jenis kemasan dan string
<bean ID = "Personservice"> <!-Tipe Dasar, Jenis String-> <Properti Nama = "Age" Value = "20"> </property> <Property Name = "name" value = "zhang wuji"> </property> </ bean>
B. mengutip kacang lainnya
<bean id = "person" /> <bean id = "personservice"> <name properti = "orang" ref = "person" /> </ bean>
1.1 Koleksi Daftar Perakitan
<name properti = "list"> <list> <value> list1 </ value> <value> list2 </ value> <ref bean = "person"/> </list> </propert>
1.2 Koleksi Set Majelis
<name properti = "set"> <set> <value> list1 </ value> <value> list2 </ value> <ref bean = "person"/> </set> </propert>
1.3 Peta Perakitan
<name properti = "peta"> <pap> <kunci entri = "01"> <value> MAP01 </ value> </dert> <entry key = "02"> <value> MAP02 </ value> </entry> </peta> </prively>
Nilai <TREST> di peta sama dengan yang dari <list> dan <Set>, yang dapat membuat elemen atribut yang valid. Perlu dicatat bahwa nilai kunci harus string.
1.4 Properti Perakitan
<name properti = "props"> <sops> <prop key = "01"> prop1 </prop> <prop key = "02"> prop2 </prop> </props> </prop Property>
Contoh spesifik
1. Buat dua objek orang dan pelajar
Paket xgp.spring.demo; import java.util.list; import java.util.map; import java.util.properties; import java.util.set; orang kelas publik {private string pid; nama string pribadi; Siswa Pribadi; daftar daftar pribadi; set set pribadi; peta peta pribadi; Properti Pribadi Properties; objek pribadi [] objek; orang publik () {System.out.println ("orang baru"); } // hilangkan metode pengambil dan setter} paket xgp.spring.demo; siswa kelas publik {public student () {system.out.println ("siswa baru"); } public void mengatakan () {System.out.println ("Student"); }}Konfigurasikan file applicationContext.xml
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Put person and student in spring container property used to describe the property value of the Person class If it is a general property, use value to assign ref. If the property is a reference type, use ref to assign value--> <bean id="person" init-method="init" lazy-init="true"> <property name="pid" value="1"></property> <property name="name" value="Wang Ermazi"></property> <property name="student" ref="student"></property> <property name="lists"> <list> <value>list1</value> <value> list2 </ value> <ref bean = "student"/> </property> <properti name = "set"> <set> <value> set1 </ value> <value> set2 </ value> <ref bean = "student"/> </set> </questr Property = "MAP" <MAP "> <MAP/PAPER <MOTA> <MOTA> <MOVET> </ENTRY </PEVIT/POTA/POP/POTA/MOVET <MOVE> <MOVE> </ENTRY </PAPAN> </ETROT/MOVER </ETROT> bean="student"/> </entry> </map> </property> <property name="properties"> <props> <!-- No reference type required--> <prop key="prop1">prop1</prop> <prop key="prop2">prop2</prop> </props> </property> <property name="objects"> <list> <value>aa</value> <value>bb</value> </list> </pruptent> </tact> <bean id = "student"> </ bean> </tac>
Tulis kelas tes Dixmlsettertest
Paket xgp.spring.test; impor org.junit.test; impor org.springframework.context.applicationContext; Impor org.springframework.context.support.classpathxmlicationContext; Impor xgp.spring.demo.person; COLPLEXCLICATION; 1. Apa yang dilakukan Container Musim Semi? (1) Mulai wadah musim semi* (2) Buat objek untuk dua kacang orang dan siswa* (3) menganalisis atribut nama properti, menyambungkan metode setter, menguraikan nilai* nilai atau referensi referensi dari properti, meneruskan parameter ke metode setter, dan menggunakan teknologi refleksi untuk menetapkan nilai ke objek. * (4) Ekstrak objek dari wadah pegas dan panggil metode objek. * 2. Apa urutan eksekusi wadah musim semi? */ @Test public void test1 () {applicationContext context = new classpathxmlapplicationContext ("applicationContext.xml"); Orang orang = (orang) context.getBean ("orang"); System.out.println (person.getPid ()); System.out.println (person.getName ()); System.out.println (person.getLists ()); System.out.println (person.getsets ()); System.out.println (person.getMap ()); System.out.println (person.getObjects (). Panjang); }}/*1 wang wu [list1, list2, xgp.spring.demo.student@76a9b9c] [set1, set2, xgp.spring.demo.student@76a9b9c] {entri1 = map1, entry2 = map2} 2*/ Urutan eksekusi kontainer musim semi
1. Semua adalah pengaturan default
2. Set Student (malas-init = true)
3. Set Person (malas-init = true)
Meringkaskan
Ada dua metode untuk menyuntikkan parameter. Konstruktor perlu menulis konstruktor yang sesuai, setter perlu menghasilkan metode setter yang sesuai, dan konstruktor default ditulis.
2.5 pentingnya IOC dan DI
Setelah mempelajari ini, apa arti menemukannya? Mari kita tulis contoh sistem manajemen dokumen untuk menggambarkan persyaratan. Lihat gambar di bawah ini
1. Tulis antarmuka dokumen
dokumen antarmuka publik {public void read (); public void write ();}2. Tulis Kelas Implementasi WordDocument, ExcelDocument, PDFDocument
Kelas publik WordDocument mengimplementasikan dokumen {public void read () {System.out.println ("Word Read"); } public void write () {System.out.println ("Word write"); }}3. Tulis Sistem DocumentManager
DocumentManager kelas publik {dokumen privat; setDocument public void (dokumen dokumen) {this.document = dokumen; } public documentManager () {} public documentManager (dokumen dokumen) {super (); this.document = dokumen; } public void read () {this.document.read (); } public void write () {this.document.write (); }}4. Tulis DocumentTest kelas tes
/*** Menggunakan IOC dan DI, Anda dapat mencapai pemrograman berorientasi antarmuka lengkap** /kelas publik DocumentTest { /*** Document Document = new WordDocument (); * Baris kode ini tidak sepenuhnya diprogram berorientasi antarmuka, karena kelas tertentu muncul di sisi kanan dari tanda yang sama*/ @test public void testDocument_nospring () {dokumen dokumen = wordDocument baru (); DocumentManager DocumentManager = new DocumentManager (dokumen); DocumentManager.read (); DocumentManager.write (); } /*** Di sisi kode, saya tidak tahu siapa yang mengimplementasikan dokumen tersebut. Ini ditentukan oleh file konfigurasi Spring * <bean id = "documentManager"> <!-Dokumen adalah antarmuka-> <name properti = "document"> <!-WordDocument adalah kelas implementasi, yang ditugaskan ke antarmuka dokumen-> <Ref bean = "PDFDocument"/> </sreanter> </bean> */@testext publiclicling (Property) </bean>/@testext public uji (/@texpument {@tdocument publocument {@texument { ClasspathxmlapplicationContext ("applicationContext.xml"); DocumentManager DocumentManager = (DocumentManager) Context.getBean ("DocumentManager"); DocumentManager.read (); DocumentManager.write (); }}Dari yang di atas, kita dapat melihat perbedaan antara tidak menerapkan pegas dan menerapkan pegas
<!-- documentManager,wordDocument,excelDocument,pdfDocument is put into spring container --> <bean id="wordDocument"></bean> <bean id="excelDocument"></bean> <bean id="pdfDocument"></bean> <bean id="pdfDocument"></bean> <bean id="documentManager" > <!-Dokumen adalah antarmuka-> <properti name = "document"> <!-WordDocument adalah kelas implementasi, yang ditugaskan ke antarmuka dokumen-> <ref bean = "pdfdocument"/> </property> </ bean>
Menggunakan Spring hanya memerlukan konfigurasi objek <Ref bean = ""> yang sesuai di ApplicationContext, tanpa memperhatikan kelas implementasi tertentu dan mengimplementasikan pemrograman yang berorientasi antarmuka lengkap. Inilah sebabnya mengapa Spring dapat berintegrasi dengan begitu banyak alat.
2.6 MVC instance simulasi structs2
Deskripsi persyaratan
Buat Direktori Proyek
Coding:
1. Buat lapisan DAO untuk membangun antarmuka persondao dan mengimplementasikan kelas persondaoImpl
Public Interface persondao {public void saveperson ();} kelas publik persondaoImpl mengimplementasikan persondao {@override public void saveperson () {System.out.println ("Save Person"); }}2. Tetapkan Lapisan Layanan, Antarmuka Personservice dan Kelas Implementasi PersonserviceImpl
Public Interface Personservice {public void saveperson ();} Public Class PersonserviceImpl mengimplementasikan Personservice {private persondao persondao; public void setpersondao (persondao persondao) {this.persondao = persondao; } @Override public void saveperson () {this.persondao.saveperson (); }}3. Buat Tindakan, Kelas Persamaan
Persamaan Public Class {Private Personservice Personservice; public void setpersonservice (Personservice Personservice) {this.personservice = Personservice; } public void saveperson () {this.personservice.saveperson (); }}4. Konfigurasikan ApplicationContext.xml
<!-- Put the service,dao,action class into the spring container--> <bean id="personDao"></bean> <bean id="personService"> <property name="personDao"> <ref bean="personDao"/> </property> </bean> <bean id="personAction"> <property name="personService"> <ref bean="personService"/> </propt> </tact>
5. Tulis kelas tes testmvc
kelas publik mvctest {@test public void testMvc () {applicationContext context = new ClassPathXMLapPlicationContext ("ApplicationContext.xml"); Personalcaction Personalction = (Persamaan) Context.getBean ("Persamaan"); feduction.saveperson (); // Save Person}}Contoh di atas jelas menunjukkan pemrograman musim semi yang berorientasi antarmuka. Lapisan layanan hanya perlu memanggil antarmuka lapisan DAO, tetapi tidak perlu memperhatikan kelas implementasi lapisan DAO, dan tindakan hanya perlu memanggil antarmuka layanan, tetapi tidak perlu memperhatikan kelas implementasi layanan.
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.