The meaning of Springioc and di

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.