Meaning of springioc and DI :
Full interface-oriented programming is achieved.
Document Case
MVC Case
Document Case - Dependency Injection using construction methods
Interface
public interface Document {void read (); void write ();}
Implementation Class 1
public class Worddocument implements Document {public void read () {System.out.println ("word read"); } public void Write () {System.out.println ("word writer"); }}
Implementation Class 2
public class Pdfdocument implements Document {public void read () {System.out.println ("pdf read"); } public void Write () {System.out.println ("pdf write"); }}
Tool class
public class Documentmanager {public document document; Public Documentmanager (document document) {this.document = document; } public void Read () {document.read (); } public void Write () {document.write (); }}
Test
public class Test1 {@Test public void test1 () {ApplicationContext context = new Classpathxmlapplicationcont Ext ("Applicationcontext.xml"); Documentmanager manager = (Documentmanager) context.getbean ("Documentmanager"); Manager.read (); Manager.write (); }}
Spring configuration file Applicationcontext.xml
<bean id= "Documentmanager" class= "spring_ioc_di_document. Documentmanager "><constructor-arg index=" 0 "ref=" pdfdocument "></constructor-arg></bean>< Bean id= "worddocument" class= "spring_ioc_di_document. Worddocument "></bean><bean id=" pdfdocument "class=" spring_ioc_di_document. Pdfdocument "></bean>
MVC case-Using Set mode dependency Injection
DAO interface
Public interface Persondao {public void read ();
DAO Implementation
public class Persondaoimpl implements Persondao {public void read () {System.out.println ("Persondao read"); }}
Service Interface
Public interface Personservice {public void read ();
Service Implementation
public class Personserviceimpl implements personservice{Persondao Persondao; Public Persondao Getpersondao () {return persondao; } public void Setpersondao (Persondao persondao) {This.persondao = Persondao; } @Override public void Read () {persondao.read (); }}
Test
public class Test2 {@Test public void test2 () {ApplicationContext context = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); Personaction action = (personaction) context.getbean ("Personaction"); Action.read (); }}
Spring configuration file Applicationcontext.xml
<bean id= "Persondao" class= "SPRING_MVC. Persondaoimpl "></bean> <bean id=" Personservice "class=" SPRING_MVC. Personserviceimpl "><property name=" Persondao "><ref bean=" Persondao "/></property><bean id= "Personaction" class= "SPRING_MVC. Personaction "><property name=" service "><ref bean=" Personservice "/></property></bean>
The meaning of Springioc and di