Spring: A Developer's Notebook笔记和小结(4)
来源:互联网 发布:长恨歌五色石南叶 知乎 编辑:程序博客网 时间:2024/05/08 20:07
/**
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2005-12-29
备注:转载请保留以上声明
**/
本篇主要介绍使用Spring来进行依赖注入,以及对以前代码的改进。
如果你机器上没有安装Spring,需要先到官方网站(http://www.springframework.org)上去下载,将Spring的lib放到war/WEB-INF/lib下,让应用能够找到它们。
将现有应用程序转移到一个设计良好的POJO的基于Spring的应用其实很简单,只需要以下几个步骤:
1 利用依赖注入的思想对现有代码进行重构。model对象看作bean,service看作aspect。一般我们只有bean,而没有使用到service。
2 去掉代码中实例化对象和设置依赖(即通过set方法设置)的那部分代码。
3 创建一个配置文件来描述bean和aspect。
4 通过Spring来访问我们的代码。
下面我们就按照上面的步骤来改进我们的程序:
先看看管理我们bean和aspect的配置文件,目前这里没有用到aspect。
Example 1-9. RentABike-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="rentaBike" class="ArrayListRentABike">
<property name="storeName"><value>"Bruce's Bikes"</value></property>
</bean>
<bean id="commandLineView" class="CommandLineView">
<property name="rentaBike"><ref bean="rentaBike"/></property>
</bean>
</beans>
这里的<bean id="rentaBike" class="ArrayListRentABike">,后面的class是具体类,实际情况可能需要完整的包名+类名的形式,这里使用的默认包。而bean前面的id用来唯一标示这个bean。
下面的<property name="storeName"><value>"Bruce's Bikes"</value></property>是给这个bean设置属性,这里就是所谓依赖注入的思想。属性名和值可以被动态的set到相应的bean中去。下面这个commandLineView也一样,只不过<ref bean="rentaBike"/>表示它有个属性是rentaBike bean的一个引用。大家如果不明白,后面会讲到具体的实现。
接着来看看这个改进后的装配器:
Example 1-10. RentABikeAssembler.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RentABikeAssembler {
public static final void main(String[] args) {
//使用这一句来读取上面定义的配置文件
ClassPathXmlApplicationContext ctx = new
ClassPathXmlApplicationContext("RentABikeApp-context.xml");
//完成依赖注入
CommandLineView clv =
(CommandLineView)ctx.getBean("commandLineView");
clv.printAllBikes( );
}
}
它使用ClassPathXmlApplicationContext("RentABikeApp-context.xml")来读取配置文件,Spring其实有很多方法来读取配置文件,后面会陆续介绍的。
ctx.getBean("commandLineView");这一句先到配置文件中去找id为commandLineView的bean,这个时候找到了其具体类是CommandLineView,并将属性和值注入到这个对象中。
我们回顾一下改动前的代码:
public class RentABikeAssembler {
public static final void main(String[] args) {
CommandLineView clv = new CommandLineView( );//1
RentABike rentaBike = new ArrayListRentABike("Bruce's Bikes");//2
clv.setRentaBike(rentaBike);//3
clv.printAllBikes( );
}
}
大家可以发现上面我注释的3句是被替换掉了,这些操作是由Spring框架来完成了依赖注入。
依赖注入的好处就是可以通过修改配置文件来给类动态加载相关的属性和引用,而不用修改代码。
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2005-12-29
备注:转载请保留以上声明
**/
本篇主要介绍使用Spring来进行依赖注入,以及对以前代码的改进。
如果你机器上没有安装Spring,需要先到官方网站(http://www.springframework.org)上去下载,将Spring的lib放到war/WEB-INF/lib下,让应用能够找到它们。
将现有应用程序转移到一个设计良好的POJO的基于Spring的应用其实很简单,只需要以下几个步骤:
1 利用依赖注入的思想对现有代码进行重构。model对象看作bean,service看作aspect。一般我们只有bean,而没有使用到service。
2 去掉代码中实例化对象和设置依赖(即通过set方法设置)的那部分代码。
3 创建一个配置文件来描述bean和aspect。
4 通过Spring来访问我们的代码。
下面我们就按照上面的步骤来改进我们的程序:
先看看管理我们bean和aspect的配置文件,目前这里没有用到aspect。
Example 1-9. RentABike-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="rentaBike" class="ArrayListRentABike">
<property name="storeName"><value>"Bruce's Bikes"</value></property>
</bean>
<bean id="commandLineView" class="CommandLineView">
<property name="rentaBike"><ref bean="rentaBike"/></property>
</bean>
</beans>
这里的<bean id="rentaBike" class="ArrayListRentABike">,后面的class是具体类,实际情况可能需要完整的包名+类名的形式,这里使用的默认包。而bean前面的id用来唯一标示这个bean。
下面的<property name="storeName"><value>"Bruce's Bikes"</value></property>是给这个bean设置属性,这里就是所谓依赖注入的思想。属性名和值可以被动态的set到相应的bean中去。下面这个commandLineView也一样,只不过<ref bean="rentaBike"/>表示它有个属性是rentaBike bean的一个引用。大家如果不明白,后面会讲到具体的实现。
接着来看看这个改进后的装配器:
Example 1-10. RentABikeAssembler.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RentABikeAssembler {
public static final void main(String[] args) {
//使用这一句来读取上面定义的配置文件
ClassPathXmlApplicationContext ctx = new
ClassPathXmlApplicationContext("RentABikeApp-context.xml");
//完成依赖注入
CommandLineView clv =
(CommandLineView)ctx.getBean("commandLineView");
clv.printAllBikes( );
}
}
它使用ClassPathXmlApplicationContext("RentABikeApp-context.xml")来读取配置文件,Spring其实有很多方法来读取配置文件,后面会陆续介绍的。
ctx.getBean("commandLineView");这一句先到配置文件中去找id为commandLineView的bean,这个时候找到了其具体类是CommandLineView,并将属性和值注入到这个对象中。
我们回顾一下改动前的代码:
public class RentABikeAssembler {
public static final void main(String[] args) {
CommandLineView clv = new CommandLineView( );//1
RentABike rentaBike = new ArrayListRentABike("Bruce's Bikes");//2
clv.setRentaBike(rentaBike);//3
clv.printAllBikes( );
}
}
大家可以发现上面我注释的3句是被替换掉了,这些操作是由Spring框架来完成了依赖注入。
依赖注入的好处就是可以通过修改配置文件来给类动态加载相关的属性和引用,而不用修改代码。
- Spring: A Developer's Notebook笔记和小结(4)
- Spring: A Developer's Notebook笔记和小结(1)
- Spring: A Developer's Notebook笔记和小结(2)
- Spring: A Developer's Notebook笔记和小结(3)
- Spring: A Developer's Notebook笔记和小结(5)
- Spring: A Developer's Notebook笔记和小结(6)
- Spring: A Developer's Notebook笔记和小结(7)
- Spring: A Developer's Notebook笔记和小结(8)
- Spring: A Developer's Notebook笔记和小结(9)
- Spring: A Developer's Notebook笔记和小结(10)
- Spring: A Developer's Notebook笔记和小结(11)
- Spring: A Developer's Notebook笔记和小结(12)
- Spring: A Developer's Notebook笔记和小结(13)
- [索引帖]Spring: A Developer's Notebook笔记和小结(共21篇,结束)
- [原创]Spring: A Developer's Notebook笔记和小结(14)
- [原创]Spring: A Developer's Notebook笔记和小结(15)
- [原创]Spring: A Developer's Notebook笔记和小结(16)
- [原创]Spring: A Developer's Notebook笔记和小结(17)
- Spring: A Developer's Notebook笔记和小结(2)
- Spring: A Developer's Notebook笔记和小结(3)
- 视频广告拟入侵手机
- 演示Session对象使用 范例(示例代码下载)
- EJB3,我们究竟得到了什么 (2)
- Spring: A Developer's Notebook笔记和小结(4)
- 中国互联网上演猫吞牛
- 陈天桥移情别恋网络饮料 品牌监管存风险
- 找到一个共享空间,里面有不少好的编程资料
- 今天看了《变形金刚.剧场版》,突来灵感PS了一下柯博文,但愿不要被博派看见了!
- 〖改编〗DOS命令进行网络配置
- 爱你就是爱自己
- 仰望星辰
- Linux的目录结构 linux技巧大全