spring中bean创建的细节

来源:互联网 发布:什么手机优化软件最好 编辑:程序博客网 时间:2024/05/21 05:38

        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方法时候执行,(容器用实现类)


aplicationContext.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: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">                <!-- IOC容器的配置: 要创建的所有的对象都配置在这里 -->        <bean id="user" class="cn.itcast.a_hello.User" init-method="init_user" destroy-method        ="destroy_user" scope="singleton" lazy-init="false"></bean>                <bean id="user1" class="cn.itcast.a_hello.User" scope="prototype"></bean></beans>


0 0
原创粉丝点击