Spring(3.2.3) - Beans(7): 延迟实例化

默认情况下,Spring IoC 容器启动后,在初始化过程中,会以单例模式创建并配置所有使用 singleton 定义的 Bean 的实例。通常情况下,提前实例化 Bean 是可取的,因为这样在配置中的任何错误就会很快被发现,否则可能要几个小时甚至几天后才会被发现。 有时候你可能并不想在 ApplicationContext 初始化时提前实例化某个 singleton 定义的 Bean,那么你可以将改 Bean 设置为延迟实例化。一个延迟实例化的 Bean 在第一次被请求的时候,Spring 容器才会创建该 Bean 的实例,而不是在容器启动的时候。

在 Spring 配置文件中,将 <bean/> 元素的设置 lazy-init 属性的值为 true,便可将该 Bean 定义为延迟实例化的。默认情况下,所有的 singleton Bean 都不是延迟实例化的。如果想让默认的情况下,所有的 singleton Bean 都是延迟实例化的,可以将 Spring 配置文件的根元素 <beans/> 的 default-lazy-init 属性值设置为 true。

延迟实例化的示例

Bean 的定义:

package com.huey.dream.bean;

public class ExampleBean {
    public ExampleBean(String type) {
        System.out.println("In ExampleBean Constructor, Bean type is " + type);
    }
}

Bean 的配置:

<bean id="eb1" class="com.huey.dream.bean.ExampleBean" lazy-init="true" >
    <constructor-arg name="type" value="lazyInitBean"/>
</bean>

<bean id="eb2" class="com.huey.dream.bean.ExampleBean">
    <constructor-arg name="type" value="eagerInitBean"/>
</bean>

测试方法:

@Test
public void testLazyInit() throws Exception {
    System.out.println("ApplicationContext 初始化开始!");
    ApplicationContext appCtx =
        new ClassPathXmlApplicationContext("applicationContext.xml");
    System.out.println("ApplicationContext 初始化完毕!");

    ExampleBean eb1 = appCtx.getBean("eb1", ExampleBean.class);
    ExampleBean eb2 = appCtx.getBean("eb2", ExampleBean.class);
}

结果输出:

ApplicationContext 初始化开始!
2015-5-16 16:02:58 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]1fddc31: startup date [Sat May 16 16:02:58 CST 2015]; root of context hierarchy
2015-5-16 16:02:58 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2015-5-16 16:02:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.s[email protected]b1cc87: defining beans [eb1,eb2]; root of factory hierarchy
In ExampleBean Constructor, Bean type is eagerInitBean
ApplicationContext 初始化完毕!
In ExampleBean Constructor, Bean type is lazyInitBean
时间: 2024-08-26 00:04:15

Spring(3.2.3) - Beans(7): 延迟实例化的相关文章

【Spring源码分析】原型Bean实例化过程、byName与byType及FactoryBean获取Bean源码实现

原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi=&qu

Spring 源码阅读&ndash;beans

最近买了本书,来大概学习写spring源码 一:先来段代码来测试一下. 照书上的先来试试 <?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:schem

Spring之 Auto-Wiring All Beans of Compatible Type

Auto-Wiring All Beans of Compatible Type @Autowired 注解按类型(type)依赖入住的时候,可以把类型兼容的所有类注入到数组.链表.map等集合数据结构中.如:mybatis中TypeHandler为例: package com.doctor.practice01; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.Resul

Spring(3.2.3) - Beans(8): 基于 Annotation 的配置

除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普通的 Spring Bean @Controller:标注一个控制器组件类 @Service:标注一个业务逻辑组件类 @Repository:标注一个 DAO 组件类 基于 Annotation 配置的示例 DAO 组件以 @Repository 标注: public interface UserD

Spring(3.2.3) - Beans(10): 生命周期

Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即将销毁 Bean 之间 依赖关系注入之后的行为 有三种方式可以在 Bean 的所有属性设置成功后执行特定的行为: 实现 org.springframework.beans.factory.InitializingBean 接口 使用 init-method 属性 使用 @PostConstruct

spring第一课,beans配置(中)——自动装配

bean的配置除了有手动配置,还有自动装配这一说法,具体怎么操作,如下代码事例. 自动装配可根据bean的名称与配置bean属性名称的对应进行配置,也可根据bean的类型与配置bean属性的类型进行配置. 1.自动配置 byName person类 1 package com.org.spring.beans; 2 3 public class Person { 4 private String name; 5 private int age; 6 private Car car; 7 8 @O

Spring(3.2.3) - Beans(6): 作用域

Spring 支持五种作用域,分别是 singleton.prototype.request.session 和 global session. 作用域 说明  singleton (默认作用域)单例模式,每个 Spring IoC 容器只会实例化一个使用 singleton 定义的 Bean.  prototype  原型模式,  request  对于每次 HTTP 请求,使用 request 定义的 Bean 都产生一个新实例只有在 Web 应用中使用 Spring 时,该作用域才有效.

依赖注入在 dotnet core 中实现与使用:3 使用 Lazy&lt;T&gt; 延迟实例化

有些对象我们并不想一开始就实例化,由于性能或者功能的考虑,希望等到使用的时候再实例化.考虑存在一个类 A, 它使用了依赖的类 B,在 A 中,只有某些不常用到的方法会涉及调用 B 中的方法,多数情况下,并不使用这个 B 的实例. using System; public class A { private B _b; public A (B b) { _b = b; Console.WriteLine("construct class A..."); } public void Met

Spring(3.2.3) - Beans(9): @Resoure &amp; @Autowired

@Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上,但不可以标注在构造方法上. 默认情况下,@Resource 按依赖的名称进行装配.@Resource 与 <property/> 元素的 ref 属性的效果相同. @Resource 的装配顺序 如果同时指定了 name 和 type 属性,则从 Spring 容器中查找唯一匹配的 Bean 进