spring boot 启动.docx
- 文档编号:8043574
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:16
- 大小:26.35KB
spring boot 启动.docx
《spring boot 启动.docx》由会员分享,可在线阅读,更多相关《spring boot 启动.docx(16页珍藏版)》请在冰豆网上搜索。
springboot启动
接触到新项目,里面的用的是spring4.0,故项目启动用了springboot
code:
Java代码
1.package com.vcredit.jdev.obiz;
2.
3./*
4. * #%L
5. * obiz
6. * %%
7. *
8. * %%
9. * This program is free software:
you can redistribute it and/or modify
10. * it under the terms of the GNU General Public License as
11. * published by the Free Software Foundation, either version 2 of the
12. * License, or (at your option) any later version.
13. *
14. * This program is distributed in the hope that it will be useful,
15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17. * GNU General Public License for more details.
18. *
19. * You should have received a copy of the GNU General Public
20. * License along with this program. If not, see
21. * //www.gnu.org/licenses/gpl-2.0.html>. 22. * #L% 23. */ 24. 25.import org.springframework.boot.SpringApplication; 26.import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 27.import org.springframework.boot.builder.SpringApplicationBuilder; 28.import org.springframework.boot.context.web.SpringBootServletInitializer; 29.import org.springframework.boot.orm.jpa.EntityScan; 30.import org.springframework.context.annotation.ComponentScan; 31.import org.springframework.context.annotation.Configuration; 32.import org.springframework.context.annotation.Import; 33. 34.@Configuration 35.@Import(net.gplatform.sudoor.server.Application.class) 36.@EnableAutoConfiguration 37.@ComponentScan 38.@EntityScan 39.public class Application extends SpringBootServletInitializer { 40. /* 41. * Used by spring boot 42. */ 43. public static void main(String[] args) { 44. SpringApplication.run(Application.class, args); 45. } 46. 47. @Override 48. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 49. return application.sources(Application.class); 50. } 51. 52.} @Import(net.gplatform.sudoor.server.Application.class) 对应的模块: Java代码 1.package net.gplatform.sudoor.server; 2. 3./* 4. * #%L 5. * obiz 6. * %% 7. * * %% 8. * This program is free software: you can redistribute it and/or modify 9. * it under the terms of the GNU General Public License as 10. * published by the Free Software Foundation, either version 2 of the 11. * License, or (at your option) any later version. 12. * 13. * This program is distributed in the hope that it will be useful, 14. * but WITHOUT ANY WARRANTY; without even the implied warranty of 15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16. * GNU General Public License for more details. 17. * 18. * You should have received a copy of the GNU General Public 19. * License along with this program. If not, see 20. * //www.gnu.org/licenses/gpl-2.0.html>. 21. * #L% 22. */ 23. 24.import org.springframework.boot.SpringApplication; 25.import org.springframework.boot.orm.jpa.EntityScan; 26.import org.springframework.context.annotation.ComponentScan; 27.import org.springframework.context.annotation.Configuration; 28.import org.springframework.context.annotation.ImportResource; 29.import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 30. 31.@Configuration 32.@ImportResource({ "classpath: META-INF/cxf/cxf.xml", "classpath: META-INF/cxf/cxf-servlet.xml", "classpath*: META-INF/cxf/cxf-extension-*.xml", 33. "classpath*: spring/**/*-config-*.xml" }) 34.@ComponentScan 35.//TODO: Don't support multiple config, so can only config multiple value. Will change this once multiple config supported 36.@EntityScan({ "net.gplatform.sudoor.server", "com.vcredit.jdev" }) 37.@EnableJpaRepositories({ "net.gplatform.sudoor.server", "com.vcredit.jdev" }) 38.public class Application { 39. 40. /* 41. * Used by spring boot 42. */ 43. public static void main(String[] args) { 44. SpringApplication.run(Application.class, args); 45. } 46. 47.} SpringBootServletInitializer: Java代码 1./* 2. * Copyright 2012-2014 the original author or authors. 3. * 4. * Licensed under the Apache License, Version 2.0 (the "License"); 5. * you may not use this file except in compliance with the License. 6. * You may obtain a copy of the License at 7. * 8. * http: //www.apache.org/licenses/LICENSE-2.0 9. * 10. * Unless required by applicable law or agreed to in writing, software 11. * distributed under the License is distributed on an "AS IS" BASIS, 12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13. * See the License for the specific language governing permissions and 14. * limitations under the License. 15. */ 16. 17.package org.springframework.boot.context.web; 18. 19.import javax.servlet.ServletContext; 20.import javax.servlet.ServletContextEvent; 21.import javax.servlet.ServletException; 22. 23.import mons.logging.Log; 24.import mons.logging.LogFactory; 25.import org.springframework.boot.builder.ParentContextApplicationContextInitializer; 26.import org.springframework.boot.builder.SpringApplicationBuilder; 27.import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; 28.import org.springframework.context.ApplicationContext; 29.import org.springframework.web.WebApplicationInitializer; 30.import org.springframework.web.context.ContextLoaderListener; 31.import org.springframework.web.context.WebApplicationContext; 32. 33./** 34. * A handy opinionated {@link WebApplicationInitializer} for applications that starts a 35. * Spring Boot application and lets it bind to the servlet and filter mappings. If your 36. * application is more complicated consider using one of the other 37. * WebApplicationInitializers. 38. * 39. * Note that a WebApplicationInitializer is only needed if you are building a war file and 40. * deploying it. If you prefer to run an embedded container (we do) then you won't need 41. * this at all. 42. * 43. * @author Dave Syer 44. */ 45.public abstract class SpringBootServletInitializer implements WebApplicationInitializer { 46. 47. protected final Log logger = LogFactory.getLog(getClass()); 48. 49. @Override 50. public void onStartup(ServletContext servletContext) throws ServletException { 51. WebApplicationContext rootAppContext = createRootApplicationContext(servletContext); 52. if (rootAppContext ! = null) { 53. servletContext.addListener(new ContextLoaderListener(rootAppContext) { 54. @Override 55. public void contextInitialized(ServletContextEvent event) { 56. // no-op because the application context is already initialized 57. } 58. }); 59. } 60. else { 61. this.logger.debug("No ContextLoaderListener registered, as " 62. + "createRootApplicationContext() did not " 63. + "return an application context"); 64. } 65. } 66. 67. protected WebApplicationContext createRootApplicationContext( 68. ServletContext servletContext) { 69. ApplicationContext parent = null; 70. Object object = servletContext 71. .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 72. if (object instanceof ApplicationContext) { 73. this.logger.info("Root context already created (using as parent)."); 74. parent = (ApplicationContext) object; 75. servletContext.setAttribute( 76. WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); 77. } 78. SpringApplicationBuilder application = new SpringApplicationBuilder(); 79. if (parent ! = null) { 80. application.initializers(new ParentContextApplicationContextInitializer( 81. parent)); 82. } 83. application.initializers(new ServletContextApplicationContextInitializer( 84. servletContext)); 85. application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); 86. application = configure(application); 87. // Ensure error pages are registered 88. application.sources(ErrorPageFilter.class); 89. return (WebApplicationContext) application.run(); 90. } 91. 92. /** 93. * Configure the application. Normally all you would need to do it add sources (e.g. 94. * config classes) because other settings have sensible defaults. You might choose 95. * (for instance) to add default command line arguments, or set an active Spring 96. * profile. 97. * @param application a builder for the application context 98. * @see SpringApplicationBuilder 99. */ 100. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 101. return application; 102. } 103. 104.} Java代码 1.WebApplicationInitializer Java代码 1./* 2. * Copyright 2002-2012 the original author or authors. 3. * 4. * Licensed under the Apache License, Version 2.0 (the "License"); 5. * you may not use this file except in compliance with the License. 6. * You may obtain a copy of the License at 7. * 8. * http: //www.apache.org/licenses/LICENSE-2.0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring boot 启动
![提示](https://static.bdocx.com/images/bang_tan.gif)