在spring的IOC容器中,可以通過設置<beans default-lazy-init="XXX"></beans>來設置是否為懶加載模式,懶加載的意思就是說是否在spring容器加載的時候將bean加載到容器中。在沒有設置的情況下,默認是false的,就是說不使用懶加載模式。
當設置為false(或者沒有設置的時候)tomcat啟動的時候出現的是:
Initializing Spring root WebApplicationContext log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 測試spring的lazy屬性 2015-10-13 19:06:04 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080 2015-10-13 19:06:04 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8009 2015-10-13 19:06:04 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/17 config=null 2015-10-13 19:06:04 org.apache.catalina.startup.Catalina start 信息: Server startup in 3546 ms
當設置true的時候,啟動tomcat:
Initializing Spring root WebApplicationContext log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 2015-10-13 19:15:44 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080 2015-10-13 19:15:44 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8009 2015-10-13 19:15:44 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/51 config=null 2015-10-13 19:15:44 org.apache.catalina.startup.Catalina start 信息: Server startup in 3912 ms
這個是測試bean
package com.yonyou.sys.utils;
//測試bean 將spring容器中設置bean的配置文件 public class TestBeanLazy { public TestBeanLazy(){ System.out.println("測試spring的lazy屬性"); } }