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