| Spring入门 |
|
| 作者是 Administrator | |
| 2008-02-25 04:46:48 | |
|
马上毕业了,找工作找的闹心死了,虽说大连那噶的对日开发的软件血多,但是迫于对应届毕业生的需求太少,至今没有把自己卖出去, 觉得看这个内容没重点的朋友可以去 hi.baidu.com/j2eedoc 找有颜色的相同文章(这里没法给重点语句标识颜色) 要明白Spring是干什么的,或者说是Spring为了什么被创造出来,这个十分重要,简单的说就是为了“更方便的帮助我们创造对象” 来回顾一下j2ee中创建对象的几种模式(注意不是j2se): 1. 使用new 来构造对象,此处不具体说明了,坏处是接口和实现类紧密耦合 通常只是创建vo(pojo)时才使用 2. 使用Factory 工厂类来创建对象,解决了1 里 实现类和接口紧密耦合的问题,但是带来了新的问题,当接口很多时代码量很大 3. 使用Spring(其实就是个大的Factory)来构造对象 首先我们创建一个接口(我就不截图了) package com.lizhe.interf; public interface Fruit { public void eat(); } 此接口定义一个“水果”接口,然后定义了一个方法“吃” 再定义两个实现类“苹果”和“橘子” package com.lizhe.Impl; import com.lizhe.interf.Fruit; public class Apple implements Fruit { public void eat() { // TODO Auto-generated method stub System.out.println("吃苹果"); } } ---------------------------------------------------------------------------------------------------------- package com.lizhe.Impl; import com.lizhe.interf.Fruit; public class Orange implements Fruit { public void eat() { // TODO Auto-generated method stub System.out.println("吃橘子"); } } ---------------------------------------------------------------------------------------------------------- 如果我们要调用eat这个方法就要得到一个“水果”接口的实例 如上所述我们有3种方法 1. 紧密耦合的new方法 Fruit f=new Apple(); f.eat(); 2. 使用工厂的Factory 方法 Fruit f=Factory.getApple(); f.eat(); 3 使用Spring的Ioc 方法 首先配置一个Spring配置文件applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="fru" class="com.lizhe.Impl.Apple"></bean> <bean id="fru2" class="com.lizhe.Impl.Orange"></bean> </beans> 注意此配置文件只做了一件事,就是将com.lizhe.Impl.Apple和com.lizhe.Impl.Orange重新命名为fru和fru2,这里只给出了实现类,而并有提及这两个类使用的是哪两个接口,读者可能有疑问,大家往下看很容易明白 接着我们在一个普通的j2se类中测试我们的程序 package com.lizhe.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.lizhe.interf.Fruit; public class Test { public static void main(String args[]) { ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml"); Fruit f = (Fruit) ctx.getBean("fru"); Fruit f2= (Fruit) ctx.getBean("fru2"); f.eat(); f2.eat(); } } 注意下面这两句 Fruit f = (Fruit) ctx.getBean("fru"); Fruit f2= (Fruit) ctx.getBean("fru2"); 这两句使用fru和fru2就得到了Apple和Orange的实例,并且标注了他们返回的实例的接口类型是fruit,当我们要修改项目的时候,只需要修改applicationContext.xml中的 <bean id="fru" class="com.lizhe.Impl.Apple"></bean> <bean id="fru2" class="com.lizhe.Impl.Orange"></bean> 这两句就可以获得不同的实例,大大降低了代码的耦合度,当接口很多的时候也不会产生很大的代码量,这正是Spring的方便所在 不知道不觉凌晨4点了,虽然躺着打字真不舒服,不过还是要补充两句 1 对于控制反转这个名字感觉有些莫名其妙,没弄明白它反转了什么,好像是说“用什么容器就构造什么”,而不是“容器用什么自己就得构造什么” 2 依赖注入 个人认为比较恰当,就是说“组建之间的依赖关系由容器运行时才决定,由容器动态的将某种依赖关系注入到组件当中”,当程序没有运行,脱离容器的时候,他们几乎可以被认为是没有任何耦合关系的,只有当程序运行时,容器调用XML配置文件的时候,才将两个程序关联在一起。这大大降低了代码间的耦合程度!也就是SPRING的精髓所在。 一开始看书的时候感觉很晦涩,其实就是一张纸,说破了其实很容易理解。 只是.... 不知道年轻可不可以注入.... 岁月能不能够反转.... |
|
| 最近更新 ( 2008-02-25 04:46:48 ) |

