spring的DI配合接口编程

一 简介
spring开发提倡接口编程,配合di技术可以层与层的解耦。

二 项目
现在我们体验一下spring的di配合接口编程的,完成一个字母大小写转换的案例。

三 步骤
1 创建一个接口ChangeLetter
package com.hsp.inter;
public interface ChangeLetter {
        //声明一个方法
        public String change();
}


2 两个类实现接口
LowwerLetter
package com.hsp.inter;
//把小写字母-》大写
public class LowwerLetter implements ChangeLetter {
        
        private String str;
        
        
        public String change() {
                // TODO Auto-generated method stub
                return str.toLowerCase();
        }
        public String getStr() {
                return str;
        }
        public void setStr(String str) {
                this.str = str;
        }
}


UpperLetter
package com.hsp.inter;
public class UpperLetter implements ChangeLetter {
        private String str;
        
        
        public String change() {
                //把小写字母->大写
                return str.toUpperCase();
        }
        public String getStr() {
                return str;
        }
        public void setStr(String str) {
                this.str = str;
        }
}


3 把对象配置到spring容器中
<?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";>
<!-- 
<bean id="changeLette" class="com.hsp.inter.UpperLetter">
<property name="str">
<value>abcef</value>
</property>
</bean>-->
 
<bean id="changeLette" class="com.hsp.inter.LowwerLetter">
<property name="str" value="ABRTY" />
</bean>
</beans>


4 测试代码
package com.hsp.inter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/inter/beans.xml");
        //获取,不用接口
        //UpperLetter changeLetter=(UpperLetter) ac.getBean("changeLette");
        //System.out.println(changeLetter.change());
        //使用接口来访问bean
        ChangeLetter changeLetter=(ChangeLetter) ac.getBean("changeLette");
        System.out.println(changeLetter.change());
    }
}



5 测试结果
abrty

6 小结
通过上面的案例,我们可以初步体会到di配合接口编程,的确可以减少层耦合度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值