SpringBoot构建工程中的一些坑

pom配置

默认打成jar,在pom.xml中指定mainClass。

<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>

项目根目录下执行mvn package生成可执行的jar包, jar包中MANIFEST.MF文件会显示mainclass。

启动

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

@RestController @Controller + @ResponseBody;

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

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

返回一个ConfigurableApplicationContext对象

  • org.springframework.context.annotation.AnnotationConfigApplicationContext
  • org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext  -----在支持web模式下启用

加载

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

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

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

WebEnvironment

SpringApplicationinitialize方法:判定Classloader成功加载接口类"javax.servlet.Servlet"、
"org.springframework.web.context.ConfigurableWebApplicationContext"中的任意一个即设置
webEnvironment = true
150505_2Mpa_3434392.png
开启Web环境,将加载AnnotationConfigEmbeddedWebApplicationContext  

142924_kZxJ_3434392.png

---Spring-context
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable;

---Spring-web
public interface WebApplicationContext extends ApplicationContext;

---Spring-context
public abstract class AbstractApplicationContext 
extends DefaultResourceLoader implements ConfigurableApplicationContext, DisposableBean;

---Spring-context
public class GenericApplicationContext 
extends AbstractApplicationContext implements BeanDefinitionRegistry;

---Spring-web
public interface ConfigurableWebApplicationContext 
extends WebApplicationContext, ConfigurableApplicationContext;

---Spring-web
public class GenericWebApplicationContext 
extends GenericApplicationContext implements ConfigurableWebApplicationContext, ThemeSource;

---Spring-boot
public class EmbeddedWebApplicationContext extends GenericWebApplicationContext;

可内嵌容器:151603_haWr_3434392.png

外部tomcat

步骤:

  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, 如果需要本地调试可以不移除。
    184122_RQ8p_3434392.png

  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);
    		}
    
    	}
    
    }

集成mybatis

application.yml:

mybatis: 
        typeAliasesPackage: com.noob.domain
        config: classpath:mybatis-config.xml

@MapperScan(basePackages = "com.noob.core.dao") 是必须的。 

在mybatis-config.xml文件中配置:

<mappers>
        <mapper resource="mapper/ErrorCodeDetailMapper.xml"/>
        <mapper resource="mapper/ChannelPriorityMapper.xml"/>
        <mapper resource="mapper/RuleInfoMapper.xml"/>
        <mapper resource="mapper/RuleMapper.xml"/>
        <mapper resource="mapper/ChannelSpecifiedMapper.xml"/>
        <mapper resource="mapper/CreditChannelCostMapper.xml"/>
</mappers>

Q&A

错误开启webEnvironment

dubbo 2.8.4 中 依赖了 javax.servlet-api 3.1.0 , 导致判定开启webEnvironment =true;
150428_QGV2_3434392.png

启动报错:

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

183551_U2xx_3434392.png

原因:

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

解决方法:

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>

 

转载于:https://my.oschina.net/u/3434392/blog/1486298

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值