Spring系列【11】配置Bean的初始化行为

对某个Bean添加lazy-init属性:lazy-init 设置只对scop属性为singleton的bean起作用

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 7
 8     <bean id="user" class="com.lh.entity.User" lazy-init="true"></bean>
 9     <bean id="printInfo" class="com.lh.util.PrintInfo" autowire="byName">
10     </bean>
11 </beans>

在容器层次中通过在<beans/>元素上使用‘default-lazy-init‘属性来控制延迟初始化也是可能的。如下面的配置:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
    default-lazy-init="true">

    <bean id="user" class="com.lh.entity.User"></bean>
    <bean id="printInfo" class="com.lh.util.PrintInfo" autowire="byName">
    </bean>
</beans>

ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实例化(也就是依赖注入)。提前实例化意味着作为初始化过程的一部分,ApplicationContext实例会创建并配置所有的singleton bean。通常情况下这是件好事,因为这样在配置中的任何错误就会即刻被发现(否则的话可能要花几个小时甚至几天)。

时间: 2025-01-04 06:59:31

Spring系列【11】配置Bean的初始化行为的相关文章

spring 注解方式配置Bean

概要: 再classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定组件包括: @Component:基本注解,标示了一个受Spring管理的组件(可以混用,spring还无法识别具体是哪一层) @Respository:建议标识持久层组件(可以混用,spring还无法识别具体是哪一层) @Service:建议标识服务层(业务层)组件(可以混用,spring还无法识别具体是哪一层) @Con

Nginx系列-11.配置Nginx反向代理和负载均衡

Nginx系列-11.配置Nginx反向代理和负载均衡 目录 - Nginx系列 Nginx系列-1.Linux下安装Nginx Nginx系列-2.配置LNMP(Linux.Nginx.MySQL.PHP)架构 Nginx系列-3.配置Nginx虚拟主机 Nginx系列-4.Nginx日志配置及日志切割 Nginx系列-5.配置Nginx的防盗链 Nginx系列-6.配置Nginx的HTTPS Nginx系列-7.配置Nginx使用uwsgi支持web.py框架 Nginx系列-8.配置Ngi

Spring系列之装配Bean

一.概述 容器是Spring框架的核心,Spring容器使用IOC管理所有组成应用系统的组件.Spring有两种不同的容器:BeanFactory提供最简单的容器,提供了最基础的依赖注入支持,ApplicationContext建立在BeanFactory的基础之上,提供了系统构架服务如从属性文件中读取文本信息,事件传递等. 在Spring容器中拼凑Bean叫做装配,装配Bean的时候,你是在告诉容器需要哪些Bean以及容器如何使用依赖注入将它们配合在一起. 二.装配Bean 2.1  使用XM

Spring入门第一课:Spring基础与配置Bean

1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所以极大的简化了我们的开发过程.Spring的核心包括Beans.Core.Context.SpEL.对应的包的文件名形式为spring-expression-4.0.0.RELEASE.jar这样的形式.在进行Spring开发时,我们必须引入这四个核心包和一个日志包,导入Referenced Lib

Spring下如何配置bean

本次讲述项目背景: 创建Service类,Service下用到dao类.通过在Spring中配置bean,实现在项目启动时,自动加载这个类 本次只讲述配置bean的注意事项,故只给出简单实例: 创建Service: public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void init(){ ……

Spring之XML配置Bean的属性注入

Spring中XML文件配置Bean的简单示例,如下: <bean id="car" class="com.smart.ditype.Car"> <property name="color"> <value>红色</value> </property> </bean> 注:在上述例子中,<property>标签对应的属性类型是基础数据类型,Spring容器会将它的

Spring基础 快速入门spring(11) bean scope注解方式

在前面我们已经学习过spring中的bean scope, 温故而知新,这次我们将使用注解的方式来验证singleton和prototype的区别. bean scope 在spring中,bean的lifecyle大体如下所示 种类 详细 singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container. prototype Scopes a sing

Spring通过注解配置Bean

@Component: 基本注解, 标识了一个受 Spring 管理的组件@Repository: 标识持久层组件@Service: 标识服务层(业务层)组件@Controller: 标识表现层组件 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&qu

asp.net core 系列 11 配置configuration (下)

四. 文件配置提供程序AddIniFile. AddXmlFile.AddJsonFile FileConfigurationProvider 是从文件系统加载配置的基类. 以下配置提供程序专用于特定文件类型: (1) INI 配置提供程序 IniConfigurationProvider: FileConfigurationProvider (2) JSON 配置提供程序 JsonConfigurationProvider: FileConfigurationProvider (3) XML