spring开发提倡接口编程,配合di技术可以层与层的解耦
现在我们体验一下spring的di配合接口编程的,完成一个字母大小写转换的案例:
思路:
1. 创建一个接口 ChangeLetter
package com.xlc.inter;
public interface ChangeLetter {
//声明一个方法
public String change();
}
2. 两个类实现接口
package com.xlc.inter;
public class LowwerLetter implements ChangeLetter {
//把小写变为大写
private String str;
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.xlc.inter;
public class UpparLetter implements ChangeLetter {
private String str;
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. 把对象配置到spring容器中
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--
<bean id="changeLette" class="com.xlc.inter.UpparLetter">
<property name="str">
<value>abcdef</value>
</property>
</bean>-->
<bean id="changeLette" class="com.xlc.inter.LowwerLetter">
<property name="str">
<value>ABCDEFG</value>
</property>
</bean>
</beans>
4. 使用
在applicationContext中拿到bean后 转换成接口类型
package com.xlc.inter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>ApplicationContext app=new ClassPathXmlApplicationContext("com/xlc/inter/applicationContextContext.xml");
<span style="white-space:pre"> </span>ChangeLetter changeLetter=(ChangeLetter) app.getBean("changeLette");
<span style="white-space:pre"> </span>System.out.println(changeLetter.change());
<span style="white-space:pre"> </span>}
}
<strong>ChangeLetter changeLetter=(ChangeLetter) app.getBean("changeLette");中得到的对象是由applicationContext.xml中配置的</strong><pre name="code" class="html" style="font-family: Arial, Helvetica, sans-serif;"><strong><bean id="changeLette" class="com.xlc.inter.UpparLetter">class<span style="font-family: Arial, Helvetica, sans-serif;">具体反射的bean决定的</span></strong>
<span style="font-family:Arial, Helvetica, sans-serif;"><strong>而bean方法又是由</strong></span><pre name="code" class="html"><strong><property name="str">
<value>abcdef</value>
</property>根据实际情况切换的,解耦目的达到</strong>
若将applicationContext.xml中注释切换
<!--
<bean id="changeLette" class="com.xlc.inter.UpparLetter">
<property name="str">
<value>abcdef</value>
</property>
</bean>-->
<bean id="changeLette" class="com.xlc.inter.LowwerLetter">
<property name="str">
<value>ABCDEFG</value>
</property>
</bean>
<bean id="changeLette" class="com.xlc.inter.UpparLetter">
<property name="str">
<value>abcdef</value>
</property>
</bean>
<!--
<bean id="changeLette" class="com.xlc.inter.LowwerLetter">
<property name="str">
<value>ABCDEFG</value>
</property>
</bean>
-->
di配合接口编程,可以减少层(web层) 和业务层的耦合度.