Spring依赖注入与配合接口编程案例

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>

 
 


5. 输出


若将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层) 和业务层的耦合度.




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值