最近在学习SpringMvc开发,有一个提问困扰了很久,就是在Eclipse启动Tomcat需要很长时间,大概要1分多钟。
启动日志:
九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Apache Tomcat/8.0.36 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server built: Jun 9 2016 13:55:50 UTC 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server number: 8.0.36.0 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Name: Windows Server 2012 R2 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Version: 6.3 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Architecture: amd64 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Java Home: D:jdk1.8.0_65jre 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Version: 1.8.0_65-b17 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Vendor: Oracle Corporation 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_BASE: E:CaiHao4 runkJava.metadata.pluginsorg.eclipse.wst.server.core mp1 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_HOME: D: omcat-8 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.base=E:CaiHao4 runkJava.metadata.pluginsorg.eclipse.wst.server.core mp1 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.home=D: omcat-8 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dwtp.deploy=E:CaiHao4 runkJava.metadata.pluginsorg.eclipse.wst.server.core mp1wtpwebapps 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Djava.endorsed.dirs=D: omcat-8endorsed 九月 08, 2016 8:59:01 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dfile.encoding=UTF-8 九月 08, 2016 8:59:01 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:jdk1.8.0_65in;C:WindowsSunJavain;C:Windowssystem32;C:Windows;D:/jdk1.8.0_65/bin/../jre/bin/server;D:/jdk1.8.0_65/bin/../jre/bin;D:/jdk1.8.0_65/bin/../jre/lib/amd64;C:Program Files (x86)Common FilesNetSarang;D:apache-maven-3.3.3in;D:jdk1.8.0_65in;C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program FilesTortoiseSVNin;C:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Program Files (x86)Microsoft ASP.NETASP.NET Web Pagesv1.0;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Program FilesMicrosoft SQL Server120ToolsBinn;C:Program Files (x86)Microsoft SQL Server100ToolsBinnVSShellCommon7IDE;C:Program Files (x86)Microsoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100ToolsBinn;C:Program Files (x86)Microsoft SQL Server100DTSBinn;C:Program FilesMicrosoft SQL Server100DTSBinn;D:eclipse-jee-neon;;. 九月 08, 2016 8:59:02 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-nio-8080"] 九月 08, 2016 8:59:02 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 信息: Using a shared selector for servlet write/read 九月 08, 2016 8:59:02 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-nio-8009"] 九月 08, 2016 8:59:02 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 信息: Using a shared selector for servlet write/read 九月 08, 2016 8:59:02 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1384 ms 九月 08, 2016 8:59:02 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Catalina 九月 08, 2016 8:59:02 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/8.0.36 九月 08, 2016 8:59:02 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deploying configuration descriptor E:CaiHao4 runkJava.metadata.pluginsorg.eclipse.wst.server.core mp1confCatalinalocalhostweb-city.xml 九月 08, 2016 8:59:02 下午 org.apache.catalina.startup.SetContextPropertiesRule begin 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:web-city' did not find a matching property. 九月 08, 2016 8:59:05 下午 org.apache.jasper.servlet.TldScanner scanJars 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 九月 08, 2016 8:59:05 下午 org.apache.catalina.core.ApplicationContext log 信息: 1 Spring WebApplicationInitializers detected on classpath 九月 08, 2016 8:59:05 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 九月 08, 2016 8:59:05 下午 org.springframework.web.context.ContextLoader initWebApplicationContext 信息: Root WebApplicationContext: initialization started 九月 08, 2016 8:59:05 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh 信息: Refreshing Root WebApplicationContext: startup date [Thu Sep 08 20:59:05 CST 2016]; root of context hierarchy 九月 08, 2016 8:59:05 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions 信息: Registering annotated classes: [class com.caihao.city.spring.config.RootConfig] 九月 08, 2016 8:59:05 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init> 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 九月 08, 2016 8:59:06 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.2.Final} 九月 08, 2016 8:59:06 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 九月 08, 2016 8:59:06 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 九月 08, 2016 8:59:06 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 九月 08, 2016 9:00:27 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 九月 08, 2016 9:00:29 下午 org.springframework.web.context.ContextLoader initWebApplicationContext 信息: Root WebApplicationContext: initialization completed in 84077 ms 九月 08, 2016 9:00:29 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet 'dispatcher' 九月 08, 2016 9:00:29 下午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'dispatcher': initialization started 九月 08, 2016 9:00:29 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh 信息: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Sep 08 21:00:29 CST 2016]; parent: Root WebApplicationContext 九月 08, 2016 9:00:29 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions 信息: Registering annotated classes: [class com.caihao.city.spring.config.WebConfig] 九月 08, 2016 9:00:29 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init> 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 九月 08, 2016 9:00:30 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache 信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Sep 08 21:00:29 CST 2016]; parent: Root WebApplicationContext 九月 08, 2016 9:00:30 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register 信息: Mapped "{[/],methods=[GET]}" onto public java.lang.String com.caihao.city.controller.HomeController.index(org.springframework.ui.Model) 九月 08, 2016 9:00:30 下午 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler 信息: Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 九月 08, 2016 9:00:30 下午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'dispatcher': initialization completed in 1088 ms 九月 08, 2016 9:00:30 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deployment of configuration descriptor E:CaiHao4 runkJava.metadata.pluginsorg.eclipse.wst.server.core mp1confCatalinalocalhostweb-city.xml has finished in 87,789 ms 九月 08, 2016 9:00:30 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-nio-8080"] 九月 08, 2016 9:00:30 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-nio-8009"] 九月 08, 2016 9:00:30 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 88457 ms
Spring配置:
@Bean public DataSource dataSource(){ BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("jdbc.url")); dataSource.setUsername(env.getRequiredProperty("jdbc.username")); dataSource.setPassword(env.getRequiredProperty("jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); return dataSource; } @Bean public LocalSessionFactoryBean sessionFactory(DataSource dataSource){ LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMappingResources(new String[]{ "com/caihao/hibernate/caihao.hbm.xml" }); Properties props = new Properties(); props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); props.setProperty("hibernate.show_sql", "true"); props.setProperty("hibernate.jdbc.fetch_size", "50"); factoryBean.setHibernateProperties(props); return factoryBean; }
解决方法添加:
props.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
参考:http://stackoverflow.com/questions/14445838/hibernate-startup-very-slow