1.2.2 依赖注入容器/框架 Spring

Spring的简单使用

1.下载Spring Framework

http://maven.springframework.org/release/org/springframework/spring/4.2.1.RELEASE/下载spring-framework-4.2.1.RELEASE-dist.zip 。在我们的例子中,只需要一部分包:

³spring-beans-4.2.1.RELEASE.jar

³spring-context-4.2.1.RELEASE.jar

³spring-context-support-4.2.1.RELEASE.jar

³spring-core-4.2.1.RELEASE.jar

³spring-expression-4.2.1.RELEASE.jar

此外,还要下载 commons-logging-1.2.jar

解压后,将它们放入BlueJ\lib\userlib文件夹中。(参考page206).

2.Spring当作tool.God

将Spring当作tool.God一样的工具而非容器使用,即作为一个利用反射+配置文件来创建对象的工具类。

(1)有类层次IServer和Server1。

package creational.di.springDemo;
public class IServer{
    public void  say(){
        System.out.println("IServer:Hello Spring");
    }
}
package creational.di.springDemo;
public class Server1 extends IServer{
  @Override public void say(){
    System.out.println("Server1: Hello Spring");
  }
}
(2)SpringTest将利用Spring创建对象IServer和Server1。在认包下创建 spring1.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="hi" class="creational.di.springDemo.Server1" />

</beans>

另外,tool.God的配置文件中有:

1-7-Hello =creational.di.springDemo.IServer

(3)测试代码

package creational.di.springDemo;
import java.io.IOException;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.beans.factory.beanfactory;
public class SpringTest {
    public static void test() throws IOException {
        beanfactory bf = new FileSystemXmlApplicationContext("spring1.xml");
        IServer h = (IServer)bf.getBean("hi");
        h.say();
        //比较
        h = (IServer)tool.God.create("1-7-Hello");
        h.say();
    }
}  
输出

Server1: Hello Spring
IServer:Hello Spring

大材小用时,大材会显得过于繁琐。从学习设计模式的角度看,tool.God小巧适用,完成依赖注入已经足够;而且你可以将tool.God视为一个幼苗或小细胞,它可以生长或分裂出一大堆的Context、beanfactory……从而成为功能强大的框架。

3.按照XML配置文件自动装配

老板/Boss有成员变量车/Car和办公室/Office。Spring可以按照XML配置文件(或在源代码中使用Annotation(标注)特性)对Boss“自动”进行依赖注入

package creational.di.springDemo;
public class Boss { 
    private Car car; 
    private Office office; 
    public void setCar(Car car){        this.car =car;    }
    public void setoffice(Office office){  this.office =office;    }
    public Car getCar(){        return this.car;    }
    public Office getoffice(){        return this.office;    }
    @Override public String toString() { 
        return "car:" + car + "\n" + "office:" + office; 
    } 
}//<span style="font-family: Arial,Helvetica,sans-serif;">Car、Office略</span>


package creational.di.springDemo;
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test { 
    public static void main(String[] args)throws java.io.IOException{ 
        ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml");
        Boss boss = (Boss)ctx.getBean("boss"); 
        System.out.println(boss); 
    } 
}
注意,Test的 代码中并 没有创建Car和Office对象,也 没有显式地调用boss.setCar()等方法,这些工作由Spring按照配置文件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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
    <bean id="boss" class="creational.di.springDemo.Boss"> 
        <property name="car" ref="car"/> 
        <property name="office" ref="office" /> 
    </bean> 
    <bean id="office" class="creational.di.springDemo.Office"> 
        <property name="officeNo" value="002"/> 
    </bean> 
    <bean id="car" class="creational.di.springDemo.Car" scope="singleton"> 
        <property name="brand" value=" 红旗 CA72"/> 
        <property name="price" value="2000"/> 
    </bean> 
</beans> 

4. Spring按照Annotation自动装配

Spring 2.5 引入的 @Autowired 注释,它可以对源代码类成员变量、方法及构造函数进行标注,以指导Spring完成自动装配的工作。

package di.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
public class Boss2 { 
    @Autowired private Car car; 
    @Autowired private Office office; 
    //同Boss 略
}

package di.springDemo;
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext;
c class Test { 
    public static void main(String[] args)throws java.io.IOException{ 
        FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("beans2.xml");
        Boss boss = (Boss)ctx.getBean("boss"); 
        System.out.println(boss); 
        ctx.destroy();// 关闭 Spring 容器,以触发 Bean 销毁方法的执行
    } 
}
注入工作由Spring按照配置文件beans2.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.5.xsd"> 
    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> 
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

    <!-- 比较beans1,删除了 boss Bean 的属性注入配置的信息 --> 
    <bean id="boss" class="creational.di.springDemo.Boss2"/> 

    <bean id="office" class="creational.di.springDemo.Office"> 
        <property name="officeNo" value="002"/> 
    </bean> 
    <bean id="car" class="creational.di.springDemo.Car" scope="singleton"> 
        <property name="brand" value=" 红旗 CA72"/> 
        <property name="price" value="2000"/> 
    </bean> 
</beans>

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...