Spring攻略学习笔记(1)实例化Spring IoC容器

1、知识点   
        Spring提供两种IoC容器实现类型。基本的一种为Bean Factory(Bean工厂)。更高级的一种为Application Context(应用程序上下文),它是对Bean工厂的兼容和扩展。
        两种IoC容器类型的Bean配置文件相同。
          应用程序上下文提供比Bean工厂更高级的特性,同时保持基本特性的兼容。所以除非是资源有限的应用程序(例如运行于一个小脚本或者移动设备上),否则强烈推荐使用应用程序上下文。
        Bean工厂和应用程序上下文接口分别是BeanFactory和ApplicationContext。
2、代码示例
        (1)序列生成器SequenceGenerator:
        /*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

/**
 * Title: SequenceGenerator.java 类的功能说明
 * 
 * @author jackie
 * @since Apr 13, 2013 12:56:57 PM
 * @version V1.0
 */
public class SequenceGenerator {

    /**
     * @Fields prefix : 前缀
     */
    private String prefix;

    /**
     * @Fields suffix : 后缀
     */
    private String suffix;

    /**
     * @Fields initial : 初始值
     */
    private int initial;

    /**
      * @Fields counter : 计数器
      */
    private int counter;

    /**
      * 获取序列号,声明为同步,使其成为线程安全的方法
      * @author jackie  
      * @date Apr 13, 2013
      * @return String   
      */
    public synchronized String getSquence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefix);
        buffer.append(initial + counter++);
        buffer.append(suffix);
        return buffer.toString();
    }

    /**
     * @param prefix
     *            the prefix to set
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    /**
     * @param suffix
     *            the suffix to set
     */
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    /**
     * @param initial
     *            the initial to set
     */
    public void setInitial(int initial) {
        this.initial = initial;
    }
}
(2)配置文件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-3.2.xsd">
<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
<property name="prefix">
<value>30</value>
</property>
<property name="suffix">
<value>A</value>
</property>
<property name="initial">
<value>
100000
</value>
</property>
</bean>
</beans>
(3)测试程序SequenceGeneratorTest:
package com.jackie.codeproject.springrecipesnote.springioc;

import static org.junit.Assert.assertEquals;

 
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SequenceGeneratorTest {
    private ApplicationContext applicationContext;

    @Test
    public void testClassPathXmlApplicationContext() {
          // 使用ClassPathXmlApplicationContext是ApplicationContext接口的一个实现,它可以从classpath中装入一个XML配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
          // 使用getBean方法并为其传递配置的唯一的Bean名称,其返回类型为Object,须强制转型。
        SequenceGenerator sequenceGenerator = (SequenceGenerator) applicationContext.getBean("sequenceGenerator");
        String expected = "30100000A";
        String actual = sequenceGenerator.getSquence();
        assertEquals(expected, actual);
    }

    @Test
    public void testFileSystemXmlApplicationContext() {
          // FileSystemXmlApplicationContext是ApplicationContext接口的另一个实现,它从文件系统或者URL装载XML配置文件
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
                "D:\\J2EEWorkspace\\springrecipenote\\spring-ioc_11\\src\\main\\resources\\applicationContext.xml");
          // 使用getBean方法并为其传递配置的唯一的Bean名称,其返回类型为Object,须强制转型。
        SequenceGenerator sequenceGenerator = (SequenceGenerator) applicationContext.getBean("sequenceGenerator");
        String expected = "30100000A";
        String actual = sequenceGenerator.getSquence();
        assertEquals(expected, actual);
    }  

    @After
    public void after(){
        if (null != applicationContext) {
            applicationContext = null;
        }
    }
 
}
3、总结
    (1)以上注入方式是setter。
    (2)除了以上两种ApplicationContext实现以外,还有XmlWebApplicationContext和XmlPortletApplicationContext,它们仅能用于Web和入口应用程序, 最常用的是ClassPathXmlApplicationContext


原文地址:https://www.cnblogs.com/xinyuyuanm/p/3019668.html