选择显示字体大小

spring: a developer27%s notebook笔记和小结(4)

/**
作者: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=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>


<beans>

  <bean id=&quot;rentabike&quot; class=&quot;arraylistrentabike&quot;>
    <property name=&quot;storename&quot;><value>&quot;bruce's bikes&quot;</value></property>
  </bean>

  <bean id=&quot;commandlineview&quot; class=&quot;commandlineview&quot;>
    <property name=&quot;rentabike&quot;><ref bean=&quot;rentabike&quot;/></property>
  </bean>

</beans>


这里的<bean id=&quot;rentabike&quot; class=&quot;arraylistrentabike&quot;>,后面的class是具体类,实际情况可能需要完整的包名+类名的形式,这里使用的默认包。而bean前面的id用来唯一标示这个bean。
下面的<property name=&quot;storename&quot;><value>&quot;bruce's bikes&quot;</value></property>是给这个bean设置属性,这里就是所谓依赖注入的思想。属性名和值可以被动态的set到相应的bean中去。下面这个commandlineview也一样,只不过<ref bean=&quot;rentabike&quot;/>表示它有个属性是rentabike bean的一个引用。大家如果不明白,后面会讲到具体的实现。

接着来看看这个改进后的装配器:

example 1-10. rentabikeassembler.java
import org.springframework.context.support.classpathxmlapplicationcontext;

public class rentabikeassembler {
  public static final void main(string&#91;&#93; args) {
    //使用这一句来读取上面定义的配置文件
    classpathxmlapplicationcontext ctx = new
      classpathxmlapplicationcontext(&quot;rentabikeapp-context.xml&quot;);
    //完成依赖注入
    commandlineview clv =
      (commandlineview)ctx.getbean(&quot;commandlineview&quot;);
    clv.printallbikes( );
  }
}


它使用classpathxmlapplicationcontext(&quot;rentabikeapp-context.xml&quot;)来读取配置文件,spring其实有很多方法来读取配置文件,后面会陆续介绍的。
ctx.getbean(&quot;commandlineview&quot;);这一句先到配置文件中去找id为commandlineview的bean,这个时候找到了其具体类是commandlineview,并将属性和值注入到这个对象中。

我们回顾一下改动前的代码:
public class rentabikeassembler {
  public static final void main(string&#91;&#93; args) {
    commandlineview clv = new commandlineview( );//1
    rentabike rentabike = new arraylistrentabike(&quot;bruce's bikes&quot;);//2
    clv.setrentabike(rentabike);//3
    clv.printallbikes( );
  }
}


大家可以发现上面我注释的3句是被替换掉了,这些操作是由spring框架来完成了依赖注入。
依赖注入的好处就是可以通过修改配置文件来给类动态加载相关的属性和引用,而不用修改代码。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons