SpringBoot构建工程中的一些坑
博客专区 > noob_fly 的博客 > 博客详情
SpringBoot构建工程中的一些坑
noob_fly 发表于5个月前
SpringBoot构建工程中的一些坑
  • 发表于 5个月前
  • 阅读 14
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

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模式下启用

WebEnvironment

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

开启Web环境,将加载AnnotationConfigEmbeddedWebApplicationContext  

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

可内嵌容器:

外部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=/spring-boot 一致。
    <build>
        <finalName>spring-boot</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.iboxpay.loan.route" })
    @ImportResource(locations = "classpath*:spring/applicationContext-per.xml")
    @MapperScan(basePackages = "com.iboxpay.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);
    		}
    
    	}
    
    }

问题

错误开启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>

集成mybatis

application.yml:

mybatis: 
        typeAliasesPackage: com.iboxpay.loan.routing.domain
        config: classpath:mybatis-config.xml

@MapperScan(basePackages = "com.iboxpay.loan.routing.core.dao") 是必须的。每个mapper上的@Mapper不是必须的。

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

 

Spring Environment 与 disconf 的 ReloadablePropertiesFactoryBean 冲突

 

标签: Spring Boot
共有 人打赏支持
粉丝 4
博文 26
码字总数 36255
×
noob_fly
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: