SSH框架之Spring的开发步骤、bean对象创建的细节

一、Spring的开发步骤:
spring各个版本中:
在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】,如2.5版本。
在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】(如果要用依赖包,需要单独下载!)

1、源码,jar文件(spring-framework-3.2.5.RELEASE):

commons-logging-1.1.3.jar             日志
spring-beans-3.2.5.RELEASE.jar        bean节点
spring-context-3.2.5.RELEASE.jar      spring上下文节点
spring-core-3.2.5.RELEASE.jar         spring核心功能
spring-expression-3.2.5.RELEASE.jar   spring表达式相关表

以上是必须引入的5个jar文件,在项目中可以用户库管理!

2、核心配置文件: applicationContext.xml
1)Spring配置文件:applicationContext.xml / bean.xml
2)配置文件的约束条件参考:
spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
</beans>   

3、API

public class App {
    // 1. 通过工厂类得到IOC容器创建的对象
    @Test
    public void testIOC() throws Exception {
        // 之前创建对象使用
        // User user = new User();

        // 现在,把对象的创建交给spring的IOC容器
        Resource resource = new ClassPathResource("mfq/test/applicationContext.xml");
        // 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + applicationContext.xml
        BeanFactory factory = new XmlBeanFactory(resource);
        // 得到容器创建的对象
        User user = (User) factory.getBean("user");
        // 测试
        System.out.println(user.getId());
    }

    //2. (方便)直接得到IOC容器对象 
    @Test
    public void testAc() throws Exception {
        // 得到IOC容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("mfq/test/applicationContext.xml");
        // 从容器中获取bean
        User user = (User) ac.getBean("user");
        // 测试
        System.out.println(user);
    }
}

二、bean对象创建的细节

    /**
     * 1) 对象创建: 单例/多例
     *    scope="singleton", 默认值, 即默认是单例    【service/dao/工具类】
     *    scope="prototype", 多例; 【Action对象】
     * 
     * 2) 什么时候创建?
     *    scope="prototype"  在用到对象的时候,才创建对象。
     *    scope="singleton"  在启动(容器初始化之前),就已经创建了bean,且整个应用只有一个。
     * 3) 是否延迟创建
     *    lazy-init="false"  默认为false,  不延迟创建,即在启动时候就创建对象
     *    lazy-init="true"   延迟初始化, 在用到对象的时候才创建对象
     *    (只对单例有效)
     * 4) 创建对象之后,初始化/销毁
     *    init-method="init_user"  【对应对象的init_user方法,在对象创建爱之后执行 】
     *    destroy-method="destroy_user"  【在调用容器对象的destriy方法时候执行,(容器用实现类)】
     */
    @Test
    public void testIOC() throws Exception {
        // 得到IOC容器对象  【用实现类(ClassPathXmlApplicationContext),因为要调用销毁的方法,而不是接口(ApplicationContext)】
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("mfq/test/applicationContext.xml");
        //测试
        System.out.println("-----容器创建-----");

        // 从容器中获取bean
        User user1 = (User) ac.getBean("user");
        User user2 = (User) ac.getBean("user");

        //测试
        System.out.println(user1);
        System.out.println(user2);

        // 销毁容器对象 
        ac.destroy();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章