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
在SpringApplication的initialize方法:判定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
步骤:
- 修改pom.xml:
打成war包的名称须和application.properties中server.context-path=/projectName 一致。<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>
<build> <finalName>projectName</finalName> </build>
spring-boot-starter-web内嵌tomcat, 如果需要本地调试可以不移除。
-
添加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>
- 启动类继承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;
启动报错:
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>