Spring 配置中的 default-lazy-init="false"

通过将Spring的default-lazy-init设置为true,可以显著减少启动时间。此设置避免了在启动时加载所有bean实例,仅在需要时加载,有效提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://thinkerandthinker.iteye.com/blog/1337706[/url]
spring的容器是提供了lazy-load的,即默认的缺省设置是bean没有lazy-load,该属性处于false状态,这样导致spring在启动过程导致在启动时候,会默认加载整个对象实例图,从初始化ACTION配置、到service配置到dao配置、乃至到数据库连接、事务等等。
这么庞大的规模,难怪spring的启动时间要花将近1分钟。尝试了一下,把beans的default-lazy-init改为true就,再次启动,速度从原来的55秒,降到8秒钟!!Great!虽然是非常小一个改动,但是影响确实非常大。

[color=red][b]Spring的default-lazy-init 与 lazy-init [/b][/color]
在同一个文件中<bean />里面设置的优先级大于<beans />里设置的优先级:
view plaincopy to clipboardprint?
1.<beans /> <bean /> immediately
2.<beans /> <bean lazy-init="true" /> lazy
3.<beans /> <bean lazy-init="false"/> immediately
4.<beans default-lazy-init="true"/> <bean /> lazy
5.<beans default-lazy-init="true"/> <bean lazy-init="true" /> lazy
6.<beans default-lazy-init="true"/> <bean lazy-init="false" /> immediately
7.<beans default-lazy-init="false"/> <bean /> immediately
8.<beans default-lazy-init="false"/> <bean lazy-init="true" /> lazy
9.<beans default-lazy-init="false"/> <bean lazy-init="false" /> immediately
如果在一个spring配置文件中引入另外的配置文件,如:<import resource="classpath:beanss.xml"/>

则以被引入文件(beanss.xml)中设置的<beans />里的设置为准,与引入文件中的设置无关。


view plaincopy to clipboardprint?
<bean id="testInit" lazy-init="true" init-method="init" class="com.test.Test">
</bean>
如果在一个spring配置文件中引入另外的配置文件,如:<import resource="classpath:beanss.xml"/>

则以被引入文件(beanss.xml)中设置的<beans />里的设置为准,与引入文件中的设置无关。


<bean id="testInit" lazy-init="true" init-method="init" class="com.test.Test">
</bean>
[b][color=red]spring的default-lazy-init参数 [/color][/b]

spring在启动的时候,会默认加载会默认加载整个对象实例图,从初始化ACTION配置、到 service配置到dao配置、乃至到数据库连接、事务等等。这样可以减少web服务器在运行时的负担,但是对于开发者来说无疑是效率极低的一个设置了。

还好,[color=red][b]spring提供了default-lazy-init属性[/b][/color],其配置形式如下,applicationContext.xml中:
< beans default-lazy-init ="true" >
< bean class ="org.xxxx.bean" >
。。。。。。
</beans>

spring配置默认default-lazy-init为false,当配置为true时sping不会再去加载整个对象实例图,大大减少了初始化的时间,减少了spring的启动速度。

这样做只是为了在开发过程中节约启动时间,在部署到实际环境中,倒是没必要设置default-lazy-init为true。毕竟部署到实际环境中不是经常的事,每次启动1分钟倒不是大问题,而且可以提高服务器效率。

当然,也不是所有的beans都能设置default-lazy-init成为true.对于scheduler的bean不能用lazy-init

< beans default-lazy-init ="true" >
< bean class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name ="triggers" >
< list >
< ref bean ="buildHtmlTrigger" />
< ref bean ="askTrigger" />
< ref bean ="mailSenderTrigger" />
< ref bean ="topicDetailBuildTrigger" />
< ref bean ="forumBuildTrigger" />
< ref bean ="topicBuildTrigger" />
</ list >
</ property >
</ bean >
</ beans >
这样的话。所有的scheduler就都不管用了。[color=red]所以请大家要注意[/color]。


[color=red][b]Spring 中lazy-init 和abstract 属性 [/b][/color]
1.lazy-init
<beans>
<bean id="service1" type="bean路径" lazy-init="true"/>
<bean id="service2" type="bean路径" lazy-init="false">
<property name="service1" ref="service1"/>
</bean>
</beans>
以上两个bean,一个lazy-init属性为true,一个为false,由什么区别呢
当 IoC容器启动时,service2会实例化,而service1则不会;但是但容器实例化service2时,service1也被实例化了,为什么呢,因为service2需要它。也就是说lazy-init="true"的bean,IoC容器启动时不会实例化该bean,只有当容器需要用到时才实例化它。lazy-init有利于容器效率,对于不需要的bean可以先不管。
2.abstract
<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
……
</bean>
bean abstract="true"时,该bean不会被实例化,上面的bean是个模板
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 对基本包进行注解扫描,开启注解 --> <context:component-scan base-package="com.esms"> <!-- 只对controller进行扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- Spring配置文件,这里主要配置和业务逻辑有关的 --> <!--=================== 数据源,事务控制,xxx ================ --> <!-- 加载数据库配置文件dbconfig.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 加载数据库 --> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${datasource.connection.driver_class}"/> <property name="jdbcUrl" value="${datasource.connection.url}"/> <property name="user" value="${datasource.connection.username}"/> <property name="password" value="${datasource.connection.password}"/> <property name="minPoolSize" value="${datasource.connection.minPoolSize}"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${datasource.connection.maxPoolSize}"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="${datasource.connection.maxIdleTime}"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="${datasource.connection.acquireIncrement}"/> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="${datasource.connection.maxStatements}"/> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="${datasource.connection.maxStatementsPerConnection}"/> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${datasource.connection.initialPoolSize}"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="${datasource.connection.idleConnectionTestPeriod}"/> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="${datasource.connection.acquireRetryAttempts}"/> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false --> <property name="breakAfterAcquireFailure" value="${datasource.connection.breakAfterAcquireFailure}"/> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="${datasource.connection.testConnectionOnCheckout}"/> <property name="checkoutTimeout" value="${datasource.connection.checkoutTimeout}"/> <property name="testConnectionOnCheckin" value="${datasource.connection.testConnectionOnCheckin}"/> <property name="automaticTestTable" value="${datasource.connection.automaticTestTable}"/> <property name="acquireRetryDelay" value="${datasource.connection.acquireRetryDelay}"/> <!--自动超时回收Connection--> <property name="unreturnedConnectionTimeout" value="${datasource.connection.unreturnedConnectionTimeout}"/> <!--超时自动断开--> <property name="maxIdleTimeExcessConnections" value="${datasource.connection.maxIdleTimeExcessConnections}"/> <property name="maxConnectionAge" value="${datasource.connection.maxConnectionAge}"/> </bean> <!--================== 配置和MyBatis的整合=============== --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="pooledDataSource"></property> <!-- 指定mybatis,mapper文件的位置 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> </bean> <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有dao接口的实现,加入到ioc容器中 --> <property name="basePackage" value="com.esms.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置一个可以执行批量的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- ===============事务控制的配置 ================ --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--控制住数据源 --> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!--配置事务增强,事务如何切入 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="SUPPORTS"/> <tx:method name="list*" read-only="true" propagation="SUPPORTS"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="query*" read-only="true" propagation="SUPPORTS"/> <tx:method name="count*" read-only="true" propagation="SUPPORTS"/> <tx:method name="update*" read-only="false" propagation="REQUIRED"/> <tx:method name="insert*" read-only="false" propagation="REQUIRED"/> <tx:method name="save*" read-only="false" propagation="REQUIRED"/> <tx:method name="delete*" read-only="false" propagation="REQUIRED"/> <tx:method name="remove*" read-only="false" propagation="REQUIRED"/> <tx:method name="*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:aspectj-autoproxy proxy-target-class="true"/> <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --> <aop:config> <!-- 切入点表达式 --> <!-- 需要修改包的扫描 --> <aop:pointcut expression="execution(* com.esms.service.impl.*.*(..))" id="txPoint" /> <!-- 配置事务增强 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" /> </aop:config> <bean class="com.esms.exception.CustomExceptionResolver"/> </beans>
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值