Recently, I was working on a large project. The project team members include about 10 project managers.Project technology is implemented using struts+spring+hibernate.Project Rules
Modules are relatively large, with a total of 10 modules, each of which is divided into dozens or even dozens of small modules.The development tool uses eclipse, since the
During the development phase, project development members frequently restart the server.When you start the server, the startup time always exceeds one minute each time.Remember doing something else before
When a project starts in less than five seconds, the difference is 10 times, and the project size is similar.
For preliminary analysis, it should be hibernate's time to interpret hbm.xml, or perhaps the spring container starts and interprets all the bean configuration files.Diagnosis
After a break, it is found that the time spent in 1 minute is mainly distributed in hibernate explaining hbm.xml for 5 seconds; the spring container from Startup to explaining the bean configuration file unexpectedly
It took 58 seconds. It was too aggressive.spring's efficiency was very questionable at that time.Attempt to search the relevant data from the Internet to see what optimization measures are available.
The first step was to find hibernate's boot optimization http://www.hibernate.org/194.html, where the main idea was to spend xml sequences locally
In a file, each time it is read, it is read from the local file and deserialized, saving hibernate xml parsing time.Tested this way
Once, it's found that hibernate's startup time has decreased from 5 to 3 seconds, but this optimization is not useful for the entire startup process.
Here is the optimization code for hibernate:
1 public Configuration addCachableFile(String xmlFile) throws MappingException { 2 try { 3 File file = new File(xmlFile); 4 File lazyfile = new File(xmlFile + ".bin"); 5 org.dom4j.Document doc = null; 6 List errors = new ArrayList(); 7 if(file.exists() && lazyfile.exists() && file.lastModified()<lazyfile.lastModified()) { 8 log.info("Mapping lazy file: " + lazyfile.getPath()); 9 ObjectInputStream oip = null; 10 oip = new ObjectInputStream(new FileInputStream(lazyfile)); 11 doc = (org.dom4j.Document) oip.readObject(); 12 oip.close(); 13 } else { 14 doc = xmlHelper.createSAXReader(xmlFile, errors, entityResolver).read( file ); 15 log.info("Writing lazy file to " + lazyfile); 16 ObjectOutputStream oup = new ObjectOutputStream(new FileOutputStream(lazyfile)); 17 oup.writeObject(doc); 18 oup.flush(); 19 oup.close(); 20 } 21 22 if ( errors.size()!=0 ) throw new MappingException( "invalid mapping", (Throwable) errors.get(0) ); 23 add(doc); 24 return this; 25 } 26 catch (Exception e) { 27 log.error("Could not configure datastore from file: " + xmlFile, e); 28 throw new MappingException(e); 29 } 30 }
Unable to do so, I looked carefully at spring's data and found that spring's container provided lazy-load, which is the default setting for bean s without lazy-load.
This property is in the false state, which causes spring to load the entire object instance diagram by default at startup, from initializing ACTION configuration,
To service configurations to dao configurations, to database connections, transactions, and so on.With such a large scale, it is no wonder that spring takes nearly a minute to start.taste
Try it, change the default-lazy-init of beans to true, and start again, slowing down from 55 seconds to 8 seconds!!Great!Very much though
A small change, but the impact is really large.Ten people in a project group, if each person needs to start the test server 50 times a day under eclipse on average.that
The project team needs to restart 500 times a day, each time saving 50 seconds, that is 25,000 seconds, nearly a few hours, almost a working day, what a great number!
But the first time you click a page during runtime, spring does lazy-load and now needs to start some of the beans you need, so it's slightly slower by 2-3
Seconds, but significantly faster than a few dozen seconds, which is worth learning.
These are spring container startup optimizations for the development phase, which do not necessarily need to be set to lazy-load when deployed to a real environment.After all, deploy to the real world
It's not a common occurrence. One minute at a time is not a big problem.
What I want to remind you here is that some beans can set default-lazy-init to true. For scheduler beans, lazy-init cannot be used
< 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 >
In that case.All scheduler s are gone.So please pay attention.Here's what you can do
< beans > < 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 >
Reproduced from: http://www.blogjava.net/baoyaer/articles/194713.html
Reprinted at: https://my.oschina.net/u/1590001/blog/268252