A dependency injection
Description: Spring promote development-oriented interface to programming, with the DI technology to achieve decoupling layer and layer.
1. Dependency injection (DI): In the application run by the outer container (Spring), will depend on the dynamic component is injected into the subject.
2. stimulate potential POJO
POJO (Plain Old Java object): simple plain Java classes.
Spring is represented by JavaBean application components, a Spring component can be any form of POJO,
it can be said POJO JavaBean is a synonym.
There is no place that it is a Spring component, Spring's non-invasive means that the programming model and non-Spring applications can play the same role in the Spring.
* Non-invasive: no forced inheritance or implement classes and interfaces spring, which means we can install or uninstall the spring at the appropriate time frame
Spring POJO given one magic way is to assemble them by DI, DI can help to maintain loose coupling of application objects between each other.
Two, DI function is how to achieve?
1, any application will be a meaningful by two or more classes, each collaboration among these classes to accomplish specific business logic. According to traditional practice, each object is responsible for managing their own mutual cooperation with the object (that is, it depends upon the object) reference, which will lead to code highly coupled and difficult to test.
2, through DI, the dependency system objects will be responsible for coordinating third-party components each object set when creating the object. Objects do not need to create your own or manage their dependencies. 3.1 dependency FIG automatically injected into the subject in need thereof to them.
Figure dependency injection will depend on the relationship automatically to the target object rather than subject themselves to get dependence
Third, actual project
Experience spring DI mating interface-oriented programming, the decoupling between the layers, a complete case letter case change of:
idea :
1, to create an interface ChangeLetter
2, implement class interface.
An implementation class: the completion of uppercase letters to lowercase letters.
Another implementation class: the completion of lowercase letters to uppercase letters.
3, the object is arranged to spring vessel.
4, use of objects, realize the function. complete test.
Specific steps :
- Defined interfaces
package com.cxh.inter;
public interface ChangeLetter {
public String change();
}
- Two implementation classes
//小写字母转换为大写字母的实现类
package com.cxh.inter;
public class UpperLetter implements ChangeLetter {
private String str;
@Override
public String change() {
return str.toUpperCase();
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str=str;
}
}
//大写字母转换为小写字母的实现类
package com.cxh.inter;
public class LowerLetter implements ChangeLetter {
private String str;
public String change() {
return str.toLowerCase();
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str=str;
}
}
- Business logic classes
package com.cxh.inter;
public class ChangBll {
private ChangeLetter chang;
public void change() {
System.out.println("ChangBll---change");
System.out.println(chang.change());
}
public void setChang(ChangeLetter chang) {
this.chang = chang;
}
}
- spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 大写变小写 -->
<bean id="change" class="com.cxh.inter.LowerLetter">
<property name="str" value="ABRTY"></property>
</bean>
<!-- 小写变大写 -->
<!-- <bean id="change" class="com.cxh.inter.UpperLetter">
<property name="str" value="abcde"></property>
</bean> -->
<bean id="changBll" class="com.cxh.inter.ChangBll">
<property name="chang" ref="change"></property>
</bean>
</beans>
- Test category
package com.cxh.inter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ChangeLetterTest {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
ChangBll changBll=(ChangBll)ac.getBean("changBll");
changBll.change();
}
}
- operation result