Programming of the Di mating interface in spring

Source: Internet
Author: User

One: di basic concept

Dependency Injection (DI), which is the foundation of the spring container implementation, is implemented in the Spring-core module. The so-called Di, refers to the object is passively accept the dependency class rather than take the initiative to find, in other words, the object is not from the container to find the class it depends on, but when the container instantiation of the object, it is actively to inject its dependent classes to it.


Di effect:

The Di Mate interface programming does reduce the coupling between the layer (Web layer) and the business layer.


Two: Di mating interface Programming case

1. Project



2. Basic code

Package com.cloud.inter;

Public Interface Changeletter {

Public String change ();

}

Package com.cloud.inter;

Public class Lowerletter Implements changeletter{

Private Stringstr;

Public String Getstr () {

return str;

}

Public void setstr (String str) {

this. str = str;

}

@Override

Public String Change () {

// TODO auto-generated Method Stub

return str. toLowerCase ();

}

}


Package com.cloud.inter;

Public class Upperletter Implements changeletter{

Private Stringstr;

Public String Getstr () {

return str;

}

Public void setstr (String str) {

this. str = str;

}

@Override

Public String Change () {

// TODO auto-generated Method Stub

return str. toUpperCase ();

}

}



3. Configure the Code

<? XML version="1.0"encoding="Utf-8"?>

< Beans xmlns="Http://www.springframework.org/schema/beans"

xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"

Xmlns:context="Http://www.springframework.org/schema/context"

xmlns:tx="Http://www.springframework.org/schema/tx"

xsi:schemalocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/ Schema/beans/spring-beans-2.5.xsd

Http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd " >

<!-- switch between the two configuration methods in the test code -

<!--

<beanid= "Changeletter" class= "Com.cloud.inter.UpperLetter" >

<propertyname= "str" >

<value>ABCEF</value>

</property>

</bean>

-

< Bean id="Changeletter"class="Com.cloud.inter.LowerLetter">

< Property name="str"value="ABCDE"/>

</ Bean >

</ Beans >


4. Test code

Package com.cloud.inter;

Importorg.springframework.context.ApplicationContext;

Importorg.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {

/**

* @param args

*/

publicstatic void Main (string[] args) {

TODO auto-generated Method Stub

Applicationcontextac=new classpathxmlapplicationcontext ("Com/cloud/inter/beans.xml");

Get not using interfaces

Upperletterchangeletter= (Upperletter) Ac.getbean ("Changeletter");

System.out.println (Changeletter.change ());

accessing beans using interfaces

Changeletterchangeletter= (Changeletter) Ac.getbean ("Changeletter");

System.out.println (Changeletter.change ());

}

}


Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Programming of the Di mating interface in spring

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.