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>
相關文章
相關標籤/搜索