SpringBoot构建Tip

pom配置

默认打成jar,在pom.xml中指定mainClass。项目根目录下执行mvn package生成可执行的jar包, jar包中MANIFEST.MF文件会显示mainclass。java

<properties>
    <mainClass>com.noob.Bootstrap</mainClass>
</properties>

<plugins> 
  <plugin> 
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-maven-plugin</artifactId>  
    <version>${spring-boot.version}</version>  
    <configuration> 
      <fork>true</fork> 
    </configuration>  
  </plugin> 
</plugins>

启动

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
    <version>1.3.6.RELEASE</version>
</dependency>

@RestController @Controller + @ResponseBody;web

@SpringBootApplication =@Configuration + @EnableAutoConfiguration + @ComponentScan;spring

  • 常规方式
    SpringApplication.run(CreditBootstrap.class, args);
  • 可设置参数
    new SpringApplicationBuilder(CreditBootstrap.class).run(args)

返回一个ConfigurableApplicationContext对象apache

  • org.springframework.context.annotation.AnnotationConfigApplicationContext
  • org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext  在支持web模式下启用(不一样版本不同)

加载

启动先加载bootstrap.yml,从远端获取application.yml覆盖本地。bootstrap

若指定scanBasePackages(包及子包下)则扫描指定路径,不然默认加载启动main方法类的包路径。api

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }, scanBasePackages={"com.noob.mics"})

WebEnvironment

(不一样版本下的实现方式和ApplicationContext不同tomcat

SpringApplicationinitialize方法:断定Classloader成功加载接口类"javax.servlet.Servlet"、
"org.springframework.web.context.ConfigurableWebApplicationContext"中的任意一个即设置
webEnvironment = true

开启Web环境,将加载AnnotationConfigEmbeddedWebApplicationContext 。app

外部tomcat

可内嵌容器:jsp

步骤:maven

  1. 修改pom.xml:
    <packaging>war</packaging>
    
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    	<version>1.3.6.RELEASE</version>
        <!-- 移除嵌入式tomcat插件 -->
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
          </exclusion>
        </exclusions>
    </dependency>
    打成war包的名称须和application.properties中server.context-path=/projectName 一致。
    <build>
        <finalName>projectName</finalName>
    </build>

    spring-boot-starter-web内嵌tomcat, 若是须要本地调试能够不移除。

  2. 添加servlet-api的依赖(二选一)

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-servlet-api</artifactId>
      <version>8.0.36</version>
      <scope>provided</scope>
    </dependency>
  3. 启动类继承SpringBootServletInitializer类:
    public abstract class SpringBootServletInitializer implements WebApplicationInitializer
    @SpringBootApplication(scanBasePackages = { "com.noob.loan.route" })
    @ImportResource(locations = "classpath*:spring/applicationContext-per.xml")
    @MapperScan(basePackages = "com.noob.loan.route.dao")
    @Slf4j
    public class RouteConsoleBootstrap extends SpringBootServletInitializer {
    
    	private static Class<RouteConsoleBootstrap> applicationClass = RouteConsoleBootstrap.class;
    
    	@Override
    	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    		return builder.sources(applicationClass);
    	}
    
    	/**
    	 * 还有一种:in a single line in application.properties:
    	 * server.context_parameters.p-name=-value
    	 * 
    	 */
    	@Override
    	public void onStartup(ServletContext servletContext) throws ServletException {
    		servletContext.setAttribute("contextConfigLocation",
               "classpath*:applicationContext-per.xml"); // 测试结果该设置无效,设置先后都为<NONE>
    		servletContext.setAttribute("failUrl", "/");
    		servletContext.setAttribute("unauthorizedUrl", "/deny.jsp");
    		servletContext.setAttribute("notFilterUrl", "");
    		super.onStartup(servletContext);
    	}
    
    	/** 本地调试使用 **/
    	public static void main(String[] args) {
    		try {
    			new SpringApplicationBuilder(applicationClass).run(args);
    			Object lock = new Object();
    			synchronized (lock) {
    				try {
    					while (true)
    						lock.wait();
    				} catch (Exception e) {
    					log.error("application running exception.", e);
    				}
    			}
    		} catch (Exception ex) {
    			log.error("start application exception.", ex);
    		}
    
    	}
    
    }

Q&A

错误开启webEnvironment

dubbo 2.8.4 中 依赖了 javax.servlet-api 3.1.0 , 致使断定开启webEnvironment =true;

启动报错:

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
	... 8 common frames omitted

解决方法:  关闭Web环境

new SpringApplicationBuilder(CreditBootstrap.class).web(false).run(args);

打war包报错:提示缺乏web.xml

缘由

spring boot项目中引用了依赖包spring-boot-starter-web。该包中引用的spring-boot-starter-tomcat里包含了tomcat嵌入式servlet容器,其不一样版本实现的是不一样的servlet版本规范。

解决方法

servlet 3.0 如下的必须有WEB-INF/web.xml;

servlet 3.0 以上(包括)且没有web.xml: maven-war-plugin 下设置failOnMissingWebXml = flase;

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<failOnMissingWebXml>false</failOnMissingWebXml>
			</configuration>
		</plugin>
	</plugins>
</build>
相关文章
相关标签/搜索