最近有幾個(gè)小項(xiàng)目需要進(jìn)行相關(guān)技術(shù)預(yù)研,使用了Spring Boot這套框架贮懈。
整個(gè)過程有一些零散,從頭入門的教程網(wǎng)上很多了醇王,就不再重復(fù)寫了。只是把自己認(rèn)為有重要的或者有坑的地方記錄下來垂谢。
1.引用Spring Boot 版本及基礎(chǔ)配置
這里使用Spring IO Platform 項(xiàng)目厦画,看介紹是可以減輕包管理版本的難度疮茄,直接按照官網(wǎng)加入進(jìn)來沒感覺什么不適滥朱。這種方式所有引入的依賴都不需要聲明版本。按照這個(gè)配置引入Spring Boot 的版本是1.5.9.RELEASE力试。
pom.xml代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.liveup</groupId>
<artifactId>platform-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
其他項(xiàng)目直接將這個(gè)項(xiàng)目做parent就可以了徙邻。
<parent>
<groupId>com.jc.platform</groupId>
<artifactId>platform</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
2.Spring Boot 啟動(dòng)類的位置
在開發(fā)過程發(fā)現(xiàn)在Spring Boot 的啟動(dòng)類不能放根下,就是必須得有一個(gè)包畸裳。要不然啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)缰犁。因?yàn)镾pring Boot 的自動(dòng)配置,會(huì)從啟動(dòng)類所在包向下進(jìn)行查找配置。類似于@ComponentScan的配置帅容。
3.Spring Boot 啟動(dòng)方式
Spring Boot 正常啟動(dòng)(方式之一)是自己寫一個(gè)啟動(dòng)類颇象,加上@SpringBootApplication標(biāo)簽。
代碼如下:
@SpringBootApplication
public class PlatformApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(PlatformApplication.class);
application.run(args);
}
}
pom.xml 配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
這種方式就可以使用IDE工具啟動(dòng)這個(gè)類的main方法運(yùn)行或調(diào)試了并徘,比較方便遣钳,啟動(dòng)整也很快。
4.Spring Boot 項(xiàng)目打成War包放到Tomcat里運(yùn)行
如果需要放到Tomcat下運(yùn)行麦乞,這種就不行了蕴茴。
需要再增加一個(gè)類繼承SpringBootServletInitializer類。
public class ProductStartApplication extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ProductApplication.class);
}
}
此處的ProductApplication就是上個(gè)步驟中使用ProductApplication類姐直,不需要做任何改動(dòng)倦淀。
pom.xml也需要修改,在引入spring-boot-start-web中排除tomcat的相關(guān)依賴声畏。并且加入servlet的相關(guān)包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<!-- <version>3.1.0</version> -->
<scope>provided</scope>
</dependency>
這樣就可以使用maven的打包命令打包后使用Tomcat運(yùn)行了撞叽。使用IDE調(diào)試工具也正常。
5.Spring Boot 項(xiàng)目啟動(dòng)自動(dòng)運(yùn)行
1插龄、 CommandLineRunner 接口
Spring Boot 提供了一個(gè)接口CommandLineRunner能扒,在Spring Boot 項(xiàng)目啟動(dòng)后自動(dòng)運(yùn)行這個(gè)接口的實(shí)現(xiàn)類,如果有多個(gè)實(shí)現(xiàn)類辫狼,還可以指定先后順序初斑,并且在實(shí)現(xiàn)類中正常使用Spring的功能,比如注入某個(gè)類膨处、讀取配置文件等见秤。
@Component
@Order(value = 1)
public class PlatformRunner implements CommandLineRunner {
/**
* PlatformProperties是Spring boot 配置文件加載類。
*/
@Autowired
private PlatformProperties properties;
@Override
public void run(String... strings) throws Exception {
System.out.println(properties.getPlatformName() + "-" + properties.getVersion());
}
}
2真椿、實(shí)現(xiàn)ApplicationListener 接口鹃答。
public class RunListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.out.println("application start....");
}
}
ApplictionListener接口的泛型參數(shù)有幾種不同的事件:
1)ContextRefreshedEvent:當(dāng)ApplicationContext初始化或者刷新時(shí)觸發(fā)該事件。
2)ContextClosedEvent:當(dāng)ApplicationContext被關(guān)閉時(shí)觸發(fā)該事件突硝。容器被關(guān)閉時(shí)测摔,其管理的所有單例Bean都被銷毀。
3)RequestHandleEvent:在Web應(yīng)用中解恰,當(dāng)一個(gè)http請(qǐng)求(request)結(jié)束觸發(fā)該事件锋八。
4)ContestStartedEvent:當(dāng)容器調(diào)用ConfigurableApplicationContext的Start()方法開始/重新開始容器時(shí)觸發(fā)該事件。
5)ContestStopedEvent:當(dāng)容器調(diào)用ConfigurableApplicationContext的Stop()方法停止容器時(shí)觸發(fā)該事件护盈。
6.自定義annotation簡化配置
目前只是定義了一個(gè)annotation,并沒有省去太多的配置挟纱,但只是一個(gè)簡單的應(yīng)用,可以進(jìn)行一步挖掘項(xiàng)目的使用方法腐宋,進(jìn)行相關(guān)的簡化紊服,達(dá)到統(tǒng)一配置的目的檀轨。
/**
* 將自定義配置文件與啟動(dòng)類簡化在一個(gè)annotation中,并且可以加入自定義的配置欺嗤。
* scanBasePackageClasses,這個(gè)配置項(xiàng)目是繼承@SpringBootApplication的参萄。
**/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@PropertySource(value = {"classpath:platform.properties"})
@EnableConfigurationProperties(PlatformProperties.class)
public @interface PlatformApplication {
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
這樣在其他引用此項(xiàng)目的其他SpringBoot 項(xiàng)目中就可以使用PlatformApplication這個(gè)注解來啟動(dòng)SpringBoot項(xiàng)目了。