model层(业务层+dao层+持久层)

spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦

举例:

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

思路如下:

  1. 创建一个接口ChangeLetter
  2. 两个类实现接口
  3. 把对象配置到spring容器中
  4. 使用

通过上面的案例,我们可以初步体会到di配合接口编程,的确可以减少层(web层)和业务层的耦合度。

思考题:

接口

ValidateUser

有一个方法

check(??)

有两个类实现不同的验证方式

CheckUser1 implements ValidateUser

{

check//到xml验证

}

CheckUser2 implements ValidateUser

{

check();//到数据库验证

}

项目结构:

beans.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"
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="changeLetter" class="com.litao.inter.UpperLetter">
<property name="str">
<value>abcdef</value>
</property>
</bean>
--> <bean id="changeLetter" class="com.litao.inter.LowwerLetter">
<property name="str" value="ABRTY" />
</bean> </beans>

UpperLetter.java

package com.litao.inter;

public class UpperLetter implements ChangeLetter {

	private String str;

	@Override
public String change() {
// TODO Auto-generated method stub
//把小写转大写
return str.toUpperCase(); } public String getStr() {
return str;
} public void setStr(String str) {
this.str = str;
} }

LowwerLetter.java

package com.litao.inter;

//把小写字母变成大写
public class LowwerLetter implements ChangeLetter { public String str; @Override
public String change() {
// TODO Auto-generated method stub
return str.toLowerCase();
} public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
} }

ChangeLetter.java

package com.litao.inter;

public interface ChangeLetter {

		//声明一个方法
public String change();
}

App1.java

package com.litao.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/litao/inter/beans.xml");
//获取,不用接口
//UpperLetter changLetter = (UpperLetter)ac.getBean("changeLetter");
//System.out.println(changLetter.change());
//使用接口来访问bean
ChangeLetter changeLetter = (ChangeLetter)ac.getBean("changeLetter");
changeLetter.change();
System.out.println(changeLetter.change()); } }

Spring框架学习之第3节的更多相关文章

  1. Spring框架学习之第2节

    传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...

  2. Spring框架学习之第1节

    spring快速入门 ①   spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...

  3. Spring框架学习之第9节

    aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...

  4. Spring框架学习之第8节

    <bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...

  5. Spring框架学习之第7节

    配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...

  6. Spring框架学习之第6节

    bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ①   实例化(当我们的程序加载 ...

  7. Spring框架学习之第5节

    request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...

  8. Spring框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...

  9. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

随机推荐

  1. 无法找到脚本文件 C:/Windows/explorer.exe:574323188.vbs

    今天打开电脑后电脑有点反常,在启动时没有运行 “局域网” 保护的程序,而且还他是 “无法找到脚本文件”如下图: 发现这个东西后,第一反应,拔掉网线.因为很有可能是中病毒了,当时就出了一身冷汗,到底是怎 ...

  2. @synthesize 有什么好处?

    如果不用 synthesize,操作的是 @property中定义的变量,使用synthesize之后,间接的操作了一个新的成员变量,到底有什么好处?直接只用一个@property不是更简单吗?

  3. C语言中链表节点的实现,以及如何实现泛型

    1.C语言中的struct是纯粹的结构体,没有访问权限的概念 2.C语言中用void* 来实现泛型编程,也是C++类和模板底层实现的基础,就是用void*来实现的 #include<stdio. ...

  4. matlab 画不同图案的柱状图

    function applyhatch(h,patterns,colorlist) %APPLYHATCH Apply hatched patterns to a figure % APPLYHATC ...

  5. 在Action中以Struts2的方式输出JSON数据

    参考地址;http://blog.csdn.net/itdada/article/details/21344985

  6. Microsoft SQL Server 存储过程举例

    -- if SP is existed, drop it. if (object_id('InvHoldToDPS', 'P') is not null) drop proc InvHoldToDPS ...

  7. 01.Redis安装

    1.安装Redis 1.下载.解压Redis [lizhiwei@localhost Redis]$ ll total 1248 -rwxrwxr-x. 1 lizhiwei lizhiwei 127 ...

  8. Linux - 常用命令行(一)

    今天和大家分享一些最基本常用命令行:也是作为新手最应该了解掌握的 ls 命令:用来显示指定工作目录下内容 dir命令:与ls命令一致 cd  命令:变化工作目录 pwd命令:显示用户当前的工作路径,显 ...

  9. ios 百度地图

    百度地图  中的注意事项 1. 百度地图中 使用了c++   设置buidSeting compoileSource 为 Object-C++  强制使用oc++编译器 2. 设置  BuidSeti ...

  10. ios读取通讯录信息

    ios读取通讯录信息 (2012-05-22 14:07:11) 标签: ios读取通讯录 it   iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和Add ...