接触到新项目,里面的用的是spring 4.0,故项目启动用了spring boot
code:
package com.vcredit.jdev.obiz; /* * #%L * obiz * %% * Copyright (C) 2013 - 2014 Shark Xu * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. * #L% */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(net.gplatform.sudoor.server.Application.class) @EnableAutoConfiguration @ComponentScan @EntityScan public class Application extends SpringBootServletInitializer { /* * Used by spring boot */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
@Import(net.gplatform.sudoor.server.Application.class)
对应的模块:
package net.gplatform.sudoor.server; /* * #%L * obiz * %% * Copyright (C) 2013 - 2014 Shark Xu * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. * #L% */ import org.springframework.boot.SpringApplication; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @ImportResource({ "classpath:META-INF/cxf/cxf.xml", "classpath:META-INF/cxf/cxf-servlet.xml", "classpath*:META-INF/cxf/cxf-extension-*.xml", "classpath*:spring/**/*-config-*.xml" }) @ComponentScan //TODO: Don't support multiple config, so can only config multiple value. Will change this once multiple config supported @EntityScan({ "net.gplatform.sudoor.server", "com.vcredit.jdev" }) @EnableJpaRepositories({ "net.gplatform.sudoor.server", "com.vcredit.jdev" }) public class Application { /* * Used by spring boot */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
SpringBootServletInitializer:
/* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.boot.context.web; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.builder.ParentContextApplicationContextInitializer; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; /** * A handy opinionated {@link WebApplicationInitializer} for applications that starts a * Spring Boot application and lets it bind to the servlet and filter mappings. If your * application is more complicated consider using one of the other * WebApplicationInitializers. * <p> * Note that a WebApplicationInitializer is only needed if you are building a war file and * deploying it. If you prefer to run an embedded container (we do) then you won't need * this at all. * * @author Dave Syer */ public abstract class SpringBootServletInitializer implements WebApplicationInitializer { protected final Log logger = LogFactory.getLog(getClass()); @Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext rootAppContext = createRootApplicationContext(servletContext); if (rootAppContext != null) { servletContext.addListener(new ContextLoaderListener(rootAppContext) { @Override public void contextInitialized(ServletContextEvent event) { // no-op because the application context is already initialized } }); } else { this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not " + "return an application context"); } } protected WebApplicationContext createRootApplicationContext( ServletContext servletContext) { ApplicationContext parent = null; Object object = servletContext .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (object instanceof ApplicationContext) { this.logger.info("Root context already created (using as parent)."); parent = (ApplicationContext) object; servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); } SpringApplicationBuilder application = new SpringApplicationBuilder(); if (parent != null) { application.initializers(new ParentContextApplicationContextInitializer( parent)); } application.initializers(new ServletContextApplicationContextInitializer( servletContext)); application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); application = configure(application); // Ensure error pages are registered application.sources(ErrorPageFilter.class); return (WebApplicationContext) application.run(); } /** * Configure the application. Normally all you would need to do it add sources (e.g. * config classes) because other settings have sensible defaults. You might choose * (for instance) to add default command line arguments, or set an active Spring * profile. * @param application a builder for the application context * @see SpringApplicationBuilder */ protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application; } }
WebApplicationInitializer
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web; import javax.servlet.ServletContext; import javax.servlet.ServletException; /** * Interface to be implemented in Servlet 3.0+ environments in order to configure the * {@link ServletContext} programmatically -- as opposed to (or possibly in conjunction * with) the traditional {@code web.xml}-based approach. * * <p>Implementations of this SPI will be detected automatically by {@link * SpringServletContainerInitializer}, which itself is bootstrapped automatically * by any Servlet 3.0 container. See {@linkplain SpringServletContainerInitializer its * Javadoc} for details on this bootstrapping mechanism. * * <h2>Example</h2> * <h3>The traditional, XML-based approach</h3> * Most Spring users building a web application will need to register Spring's {@code * DispatcherServlet}. For reference, in WEB-INF/web.xml, this would typically be done as * follows: * <pre class="code"> * {@code * <servlet> * <servlet-name>dispatcher</servlet-name> * <servlet-class> * org.springframework.web.servlet.DispatcherServlet * </servlet-class> * <init-param> * <param-name>contextConfigLocation</param-name> * <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value> * </init-param> * <load-on-startup>1</load-on-startup> * </servlet> * * <servlet-mapping> * <servlet-name>dispatcher</servlet-name> * <url-pattern>/</url-pattern> * </servlet-mapping>}</pre> * * <h3>The code-based approach with {@code WebApplicationInitializer}</h3> * Here is the equivalent {@code DispatcherServlet} registration logic, * {@code WebApplicationInitializer}-style: * <pre class="code"> * public class MyWebAppInitializer implements WebApplicationInitializer { * * @Override * public void onStartup(ServletContext container) { * XmlWebApplicationContext appContext = new XmlWebApplicationContext(); * appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); * * ServletRegistration.Dynamic dispatcher = * container.addServlet("dispatcher", new DispatcherServlet(appContext)); * dispatcher.setLoadOnStartup(1); * dispatcher.addMapping("/"); * } * * }</pre> * * As an alternative to the above, you can also extend from {@link * org.springframework.web.servlet.support.AbstractDispatcherServletInitializer}. * * As you can see, thanks to Servlet 3.0's new {@link ServletContext#addServlet} method * we're actually registering an <em>instance</em> of the {@code DispatcherServlet}, and * this means that the {@code DispatcherServlet} can now be treated like any other object * -- receiving constructor injection of its application context in this case. * * <p>This style is both simpler and more concise. There is no concern for dealing with * init-params, etc, just normal JavaBean-style properties and constructor arguments. You * are free to create and work with your Spring application contexts as necessary before * injecting them into the {@code DispatcherServlet}. * * <p>Most major Spring Web components have been updated to support this style of * registration. You'll find that {@code DispatcherServlet}, {@code FrameworkServlet}, * {@code ContextLoaderListener} and {@code DelegatingFilterProxy} all now support * constructor arguments. Even if a component (e.g. non-Spring, other third party) has not * been specifically updated for use within {@code WebApplicationInitializers}, they still * may be used in any case. The Servlet 3.0 {@code ServletContext} API allows for setting * init-params, context-params, etc programmatically. * * <h2>A 100% code-based approach to configuration</h2> * In the example above, {@code WEB-INF/web.xml} was successfully replaced with code in * the form of a {@code WebApplicationInitializer}, but the actual * {@code dispatcher-config.xml} Spring configuration remained XML-based. * {@code WebApplicationInitializer} is a perfect fit for use with Spring's code-based * {@code @Configuration} classes. See @{@link * org.springframework.context.annotation.Configuration Configuration} Javadoc for * complete details, but the following example demonstrates refactoring to use Spring's * {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext * AnnotationConfigWebApplicationContext} in lieu of {@code XmlWebApplicationContext}, and * user-defined {@code @Configuration} classes {@code AppConfig} and * {@code DispatcherConfig} instead of Spring XML files. This example also goes a bit * beyond those above to demonstrate typical configuration of the 'root' application * context and registration of the {@code ContextLoaderListener}: * <pre class="code"> * public class MyWebAppInitializer implements WebApplicationInitializer { * * @Override * public void onStartup(ServletContext container) { * // Create the 'root' Spring application context * AnnotationConfigWebApplicationContext rootContext = * new AnnotationConfigWebApplicationContext(); * rootContext.register(AppConfig.class); * * // Manage the lifecycle of the root application context * container.addListener(new ContextLoaderListener(rootContext)); * * // Create the dispatcher servlet's Spring application context * AnnotationConfigWebApplicationContext dispatcherContext = * new AnnotationConfigWebApplicationContext(); * dispatcherContext.register(DispatcherConfig.class); * * // Register and map the dispatcher servlet * ServletRegistration.Dynamic dispatcher = * container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); * dispatcher.setLoadOnStartup(1); * dispatcher.addMapping("/"); * } * * }</pre> * * As an alternative to the above, you can also extend from {@link * org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer}. * * Remember that {@code WebApplicationInitializer} implementations are <em>detected * automatically</em> -- so you are free to package them within your application as you * see fit. * * <h2>Ordering {@code WebApplicationInitializer} execution</h2> * {@code WebApplicationInitializer} implementations may optionally be annotated at the * class level with Spring's @{@link org.springframework.core.annotation.Order Order} * annotation or may implement Spring's {@link org.springframework.core.Ordered Ordered} * interface. If so, the initializers will be ordered prior to invocation. This provides * a mechanism for users to ensure the order in which servlet container initialization * occurs. Use of this feature is expected to be rare, as typical applications will likely * centralize all container initialization within a single {@code WebApplicationInitializer}. * * <h2>Caveats</h2> * * <h3>web.xml versioning</h3> * <p>{@code WEB-INF/web.xml} and {@code WebApplicationInitializer} use are not mutually * exclusive; for example, web.xml can register one servlet, and a {@code * WebApplicationInitializer} can register another. An initializer can even * <em>modify</em> registrations performed in {@code web.xml} through methods such as * {@link ServletContext#getServletRegistration(String)}. <strong>However, if * {@code WEB-INF/web.xml} is present in the application, its {@code version} attribute * must be set to "3.0" or greater, otherwise {@code ServletContainerInitializer} * bootstrapping will be ignored by the servlet container.</strong> * * <h3>Mapping to '/' under Tomcat</h3> * <p>Apache Tomcat maps its internal {@code DefaultServlet} to "/", and on Tomcat versions * <= 7.0.14, this servlet mapping <em>cannot be overridden programmatically</em>. * 7.0.15 fixes this issue. Overriding the "/" servlet mapping has also been tested * successfully under GlassFish 3.1.<p> * * @author Chris Beams * @since 3.1 * @see SpringServletContainerInitializer * @see org.springframework.web.context.AbstractContextLoaderInitializer * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer * @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer */ public interface WebApplicationInitializer { /** * Configure the given {@link ServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initializing this web application. See * examples {@linkplain WebApplicationInitializer above}. * @param servletContext the {@code ServletContext} to initialize * @throws ServletException if any call against the given {@code ServletContext} * throws a {@code ServletException} */ void onStartup(ServletContext servletContext) throws ServletException; }
这块就是用
* public class MyWebAppInitializer implements WebApplicationInitializer { * * @Override * public void onStartup(ServletContext container) { * XmlWebApplicationContext appContext = new XmlWebApplicationContext(); * appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); * * ServletRegistration.Dynamic dispatcher = * container.addServlet("dispatcher", new DispatcherServlet(appContext)); * dispatcher.setLoadOnStartup(1); * dispatcher.addMapping("/"); * } * * } 来代替传统的web.xml配置,如下:
* <servlet> * <servlet-name>dispatcher</servlet-name> * <servlet-class> * org.springframework.web.servlet.DispatcherServlet * </servlet-class> * <init-param> * <param-name>contextConfigLocation</param-name> * <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value> * </init-param> * <load-on-startup>1</load-on-startup> * </servlet> * * <servlet-mapping> * <servlet-name>dispatcher</servlet-name> * <url-pattern>/</url-pattern> * </servlet-mapping>
相关推荐
在本文中,我们将深入探讨Spring Boot启动时涉及的主要步骤,包括注解解析和启动方法的执行。 首先,我们关注`@SpringBootApplication`注解,这是Spring Boot应用程序的核心。这个注解实际上是三个关键注解的组合:...
Spring Boot启动流程.png
在深入理解Spring Boot启动过程的同时,掌握事件监听机制能帮助我们更好地优化和调试应用程序。 首先,让我们详细探讨Spring Boot的启动流程: 1. **主程序执行**:通常,Spring Boot应用通过一个标记了`@...
Spring Boot启动过程完全解析 Spring Boot启动过程是指从应用程序启动到Spring Boot应用程序完全启动的整个过程。这个过程可以分为多个阶段,每个阶段都有一些重要的步骤和机制。下面将详细地介绍Spring Boot启动...
关于标签"源码",我们可以进一步讨论Spring Boot启动过程的源码分析。Spring Boot的启动流程涉及多个组件,包括`SpringApplication`、`ApplicationContext`等。在启动过程中,`SpringApplication.run()`方法是主要...
Spring Boot启动过程(四)之Spring Boot内嵌Tomcat启动 本文主要介绍了Spring Boot启动过程(四)之Spring Boot内嵌Tomcat启动的相关知识点,用于帮助开发者更好地理解Spring Boot的启动过程。 一、...
Spring Boot 启动原理解析 Spring Boot 是一个基于 Java 的框架,它提供了一种快速和简洁的方式来开发 Web 应用程序。在 Spring Boot 中,启动类是整个应用程序的入口点,它负责初始化和配置应用程序的各种组件。在...
在Spring Boot应用开发中,"项目启动找不到启动类"是一个常见的问题,这通常是由于配置不当或者编码错误导致的。Spring Boot的设计目标是简化Spring应用程序的初始设置和配置,因此一个可运行的Spring Boot应用通常...
Spring Boot启动过程(五)之Springboot内嵌Tomcat对象的start教程详解 Spring Boot启动过程(五)之Springboot内嵌Tomcat对象的start教程详解是Spring Boot启动过程的重要组件之一,本文将详细介绍Springboot内嵌...
1.26 Spring Boot启动加载数据CommandLineRunner 1.27 Spring Boot环境变量读取和属性对象的绑定 1.28 Spring Boot使用自定义的properties 1.29 改变自动扫描的包 1.30 Spring Boot Junit单元测试 1.31 SpringBoot...
"Spring Boot启动banner定制的步骤详解" 在 Spring Boot 应用程序中,启动 banner 是可以进行定制的,这篇文章将详细介绍 Spring Boot 启动 banner 定制的步骤和方法。 首先,Spring Boot 的默认启动 banner 是...
将改脚本复制到Linux服务器,调整jar包 名称及路径,日志文件路径,赋予该文件可执行权限,执行即可启动对应spring boot 服务
2. **启动器配置**:插件能够识别并列出所有可用的Spring Boot启动器,方便用户根据项目需求选择合适的依赖项。 3. **配置文件管理**:对于application.properties或application.yml文件,插件提供了一键格式化和...
**Spring Boot启动方式详解** Spring Boot以其简洁的配置和快速的开发能力,成为了现代Java开发中的主流框架之一。本文将深入探讨Spring Boot的两种主要启动方式:jar启动和war启动,以及如何创建一个简单的Spring ...
内容概要:文章主要探讨了在Spring Boot项目启动时自动执行特定任务的两种机制:ApplicationRunner和CommandLineRunner接口的实现及其应用场景。作者不仅详细讲解了如何利用这两个接口在应用程序初始化时调用指定的...
全网内容最全,比收费教程更好的Spring Boot免费教程! 快速入门 Spring Boot 2.x基础教程:版本关系 Spring Boot 2.x基础教程:快速入门 Spring Boot 2.x基础教程:工程结构推荐 ...Spring Boot 2.x基础教程:找回启动
Spring Boot启动器ArangoDB 用于以Spring Boot方式使用的Spring Boot启动程序。目录概述该实现提供了一种使用框架的方法,例如spring boot starter项目。入门添加依赖< dependency> < groupId>io.github.ganchix...
Spring Boot Starter又称作为Spring Boot启动器,是Spring Boot框架中最核心的组件。其中可能包含自动装配模块(Autoconfigure Module)和启动器模块(Starter Module)等多种组件。自动装配模块包含类库中的每种...
总之,编写Linux上的Spring Boot启动和停止脚本是实现高效部署和管理的关键步骤。它们提供了对应用生命周期的控制,使得我们可以方便地启动、停止和监控应用,同时保持系统的整洁和有序。在实际操作中,应根据具体...
* 在 IDEA 或 Eclipse 中,直接运行应用的 Spring Boot 启动类的 #main(String[] args) 启动。 * 如果是 Web 项目,可以打包成 War 包,使用外部 Tomcat 或 Jetty 等容器。 8. Spring Boot 中的监视器是什么? ...