博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring攻略学习笔记(1)------实例化Spring IoC容器
阅读量:4472 次
发布时间:2019-06-08

本文共 3870 字,大约阅读时间需要 12 分钟。

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/archive/2013/04/14/3019668.html

你可能感兴趣的文章
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
Shell编程-条件测试 | 基础篇
查看>>
AngularJs学习笔记1——总体介绍
查看>>
C语言第十二讲,文件操作.
查看>>
绝对定位和相对定位
查看>>
实习第二天——学习mac终端命令(unix命令)和git代码管理
查看>>
微信支付
查看>>
吴裕雄--天生自然 高等数学学习:含参变量的积分
查看>>
成本的费用归集
查看>>
本周ASP.NET英文技术文章推荐[01/28 - 02/03]
查看>>
运行时库组件 RuntimePack v19.06.05 Full 纯净安装版
查看>>
NYOJ100 - 1的个数
查看>>
左侧定宽右侧自适应布局
查看>>
文件和目录的访问控制(4) 审核规则
查看>>
搭建svn的一些问题
查看>>