簡介##
目前桑腮,很多企業(yè)級的Java web應(yīng)用絕大部分都用到了spring框架泉哈,然后繁雜的xml以及annotation配置,往往使得開發(fā)花費(fèi)大量時(shí)間去配置以及維護(hù)破讨。spring boot項(xiàng)目是基于spring框架的項(xiàng)目進(jìn)行整合丛晦,使得開發(fā)能夠快速創(chuàng)建可以獨(dú)立運(yùn)行的spring應(yīng)用,Spring Boot 可以自動(dòng)配置 Spring 的各種組件提陶,并不依賴代碼生成和 XML 配置文件烫沙。Spring Boot 可以大大提升使用 Spring 框架時(shí)的開發(fā)效率。極大的減少了開發(fā)在配置上的時(shí)間隙笆,使得開發(fā)可以專注于業(yè)務(wù)邏輯锌蓄,并且springboot可以以jar的方式部署。
特性##
springboot有如下特性:
- 創(chuàng)建可以獨(dú)立運(yùn)行的spring應(yīng)用
- 直接嵌入jetty和tomcat服務(wù)器撑柔,不需要部署war
- 使用更簡潔的maven配置
- 不需要xml配置
- 提供可以直接在生產(chǎn)環(huán)境中使用的功能瘸爽,如性能指標(biāo)、應(yīng)用信息和應(yīng)用健康檢查铅忿。
環(huán)境準(zhǔn)備##
- jdk配置
- idea開發(fā)工具
- maven本地倉庫配置
工程創(chuàng)建啟動(dòng)##
1.新建一個(gè)maven工程命名spring-boot-helloworld蝶糯,如圖
2.添加spring-boot的pom依賴,需要添加倉庫地址辆沦,可以確保順利下載相關(guān)jar
<pre>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/>
</parent>
<!--版本號,使用utf8,jdk1.8-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web應(yīng)用相關(guān)包括springmvc等-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--提供自動(dòng)配置功能,此處用到了@SpringBootApplication-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<!--配置倉庫地址,就可以下載相應(yīng)的jar,如果不配置,可能會(huì)下載不了maven依賴的jar包-->
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<!--構(gòu)建springboot使用的maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></pre>
3.此時(shí)工程中的maven倉庫中jar的依賴,如圖:
4.在src/main/java/com/swun目錄下創(chuàng)建Application.java识虚,添加代碼如下
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
Class<?>[] exclude() default {};
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
5.啟動(dòng)springboot應(yīng)用肢扯,右擊->run Application.java 或者maven -clean ->maven install找到target目錄下面的spring-boot-helloworld-1.0-SNAPSHOT.jar,運(yùn)行java -jar spring-boot-helloworld-1.0-SNAPSHOT.jar
至此担锤,spring boot啟動(dòng)成功蔚晨!