spring-boot入門工程之j360-boot:(歡迎star、fork)java
https://github.com/xuminwlt/j360-bootgit
spring-boot官方地址github
http://projects.spring.io/spring-boot/
web
【j360-boot】Spring-boot系列一(多是最好的quick start) redis
【j360-boot】Spring-boot系列二(困難模式,比簡單複雜那麼一點點)
spring
【j360-boot】Spring-boot系列三(崩潰模式,不是你崩就是電腦崩)mongodb
【j360-boot】Spring-boot系列四(運維福利,監控和管理生產環境)
docker
【j360-boot】Spring-boot系列五(docker、docker、docker)shell
Spring Boot和Docker微服務+分佈式服務器架構圖設計和案例實施
數據庫
Spring Boot從目前趨勢來看已是Spring的頂級項目,這其中有其存在的必然性,在Spring大行其道的今天,spring就像快要接管了整個javaWeb開發,可是愈來愈複雜的配置、愈來愈龐大的集成使得不少人難以招架,因而就像官方的介紹那樣,spring-boot來了:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can 「just run」. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. You can use Spring Boot to create Java applications that can be started using java -jar or more traditional war deployments. We also provide a command line tool that runs 「spring scripts」. Our primary goals are: • Provide a radically faster and widely accessible getting started experience for all Spring development. • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults. • Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration). • Absolutely no code generation and no requirement for XML configuration.
Spring boot可使用多種方法構建,官方對於構建的建議:
強烈建議你選擇一個支持依賴管理,能消費發佈到Maven中央倉庫的artifacts的構建系統。咱們推薦你選擇Maven或Gradle。
選擇其餘構建系統來使用Spring Boot也是可能的(好比Ant),但它們不會被很好的支持。
在J360-boot中,使用Maven進行構建,其中會有部分說明使用gradle進行比較,好比使用yml的配置。
在學習spring boot(SB)以前,有着各類各樣的疑問,SB到底能改變什麼,會對如今的工程結構有多大的提高或者影響,有沒有必要將如今的項目遷移到SB,通過一段時間的改造和堅持,我我的認爲若是使用Spring做爲框架的JavaWeb工程,遷到到SB也是一件必然性的事情,固然其中會有很是多的工做要作,好比Servlet2.5到Servlet3.0的升級牽涉到的內容也足夠測試的了。
廢話很少說,直接上吧。
先看看工程結構,和普通maven工程沒有太大的區別,能夠說幾乎沒有區別,可是習慣上會有些官方建議的編排:
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"> <groupId>me.j360.boot</groupId> <artifactId>j360-simple</artifactId> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.BUILD-SNAPSHOT</version> </parent> <properties> <java.version>1.7</java.version> </properties> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <!-- Additional lines to be added here... --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
pom中的幾個主要節點都會 部署相關的boot信息,這些信息分別使得使用最少的配置,實如今Spring工程中自動裝配相應的配置:
一、parent:繼承spring-boot-start-parent能夠方便地繼承parent中的依賴、build和其餘信息
二、dependencies:spring-boot提供了諸多的以start結尾的各類框架的依賴,好比jpa、jms、redis等等
三、build:使用spring-boot提供的插件能夠對spring-boot工程進行打包和運行,其中會提供諸多其餘特性,好比熱加載等等
四、repo:引入官方的依賴包倉庫,xcodeghost不約不約啊
在升級版中,打交道最多的將會是第二個依賴包的管理,更好的依賴spring-boot提供的依賴包,將會減小很是多的配置,配置在每一個工程裏面,特別是隨着多場景dev、test、prod切換的時候,配置是一件不得不重視的資源,spring-boot最標準的配置文件是下面的:application.properties
名稱 描述
spring-boot-starter 核心Spring Boot starter,包括自動配置支持,日誌和YAML
spring-boot-starter-actuator 生產準備的特性,用於幫你監控和管理應用
spring-boot-starter-amqp 對"高級消息隊列協議"的支持,經過 spring-rabbit 實現
spring-boot-starter-aop 對面向切面編程的支持,包括 spring-aop 和AspectJ
spring-boot-starter-batch 對Spring Batch的支持,包括HSQLDB數據庫
spring-boot-starter-cloudconnectors
對Spring Cloud Connectors的支持,簡化在雲平臺下(例如,Cloud Foundry 和 Heroku)服務的鏈接
spring-boot-starter-dataelasticsearch對Elasticsearch搜索和分析引擎的支持,包括 spring-data-elasticsearch
spring-boot-starter-datagemfire對GemFire分佈式數據存儲的支持,包括 spring-data-gemfire
spring-boot-starter-data-jpa 對"Java持久化API"的支持,包括 spring-data-jpa , spring-orm 和Hibernate
spring-boot-starter-datamongodb對MongoDB NOSQL數據庫的支持,包括 spring-data-mongodb
spring-boot-starter-data-rest 對經過REST暴露Spring Data倉庫的支持,經過 spring-data-rest-webmvc 實現
spring-boot-starter-data-solr 對Apache Solr搜索平臺的支持,包括 spring-data-solr
spring-boot-starter-freemarker 對FreeMarker模板引擎的支持
spring-boot-starter-groovytemplates對Groovy模板引擎的支持
spring-boot-starter-hateoas 對基於HATEOAS的RESTful服務的支持,經過 spring-hateoas 實現
spring-boot-starter-hornetq 對"Java消息服務API"的支持,經過HornetQ實現
spring-boot-starter-integration 對普通 spring-integration 模塊的支持
spring-boot-starter-jdbc 對JDBC數據庫的支持
spring-boot-starter-jersey 對Jersey RESTful Web服務框架的支持
spring-boot-starter-jtaatomikos對JTA分佈式事務的支持,經過Atomikos實現
spring-boot-starter-jta-bitronix 對JTA分佈式事務的支持,經過Bitronix實現
spring-boot-starter-mail 對 javax.mail 的支持
spring-boot-starter-mobile 對 spring-mobile 的支持
spring-boot-starter-mustache 對Mustache模板引擎的支持
spring-boot-starter-redis 對REDIS鍵值數據存儲的支持,包括 spring-redis
spring-boot-starter-security 對 spring-security 的支持
spring-boot-starter-socialfacebook對 spring-social-facebook 的支持
spring-boot-starter-sociallinkedin對 spring-social-linkedin 的支持
spring-boot-starter-socialtwitter對 spring-social-twitter 的支持
spring-boot-starter-test 對經常使用測試依賴的支持,包括JUnit, Hamcrest和Mockito,還有 spring-test 模塊
spring-boot-starter-thymeleaf 對Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity 對Velocity模板引擎的支持
spring-boot-starter-web 對全棧web開發的支持,包括Tomcat和 spring-webmvc
spring-boot-starter-websocket 對WebSocket開發的支持
spring-boot-starter-ws 對Spring Web服務的支持
J360Application.java
package me.j360.boot.simple; import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; /** * Created with springbootweb -> me.j360.springboot.jar. * User: min_xu * Date: 2015/7/28 * Time: 15:28 * 說明: */ public class J360Application { public static void main(String[] args) { SpringApplication.run(J360Configuration.class, args); } }
J360Configuration.java
package me.j360.boot.simple; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; /** * Created with springbootweb -> me.j360.springboot.jar. * User: min_xu * Date: 2015/7/28 * Time: 15:28 * 說明: */ @ComponentScan @EnableAutoConfiguration public class J360Configuration { }
spring boot以default包和定位main方法進行spring boot入口的管理,在pom中使用的project的包名下稱之爲default包,spring-boot建議一個spring-boot工程使用一個main方法,即spring-boot啓動入口,通常放在這個Application類中。
@ComponentScan @EnableAutoConfiguration
這兩個註解幾乎是全部spring-boot工程的標配註解(除本身實現外),
@Configuration
更多狀況下,須要定義一個配置類,做爲默認的配置類,配置類的做用其實就是使用java方式進行配置,而不是xml方式進行的配置,spring-boot的建議其實也是消滅xml配置(不過目前仍是能夠同時使用xml配置的)。
若是你絕對須要使用基於XML的配置,咱們建議你仍舊從一個 @Configuration 類開始。你可使用附加的 @ImportResource 註解加載XML配置文件。
上述3個註解若是同時使用,可使用一個註解代替之:
@SpringBootApplication
下面開始配置web的入口,新建一個spring-mvc的類便可:
Index.java
package me.j360.boot.simple.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created with j360-boot -> me.j360.boot.simple.web. * User: min_xu * Date: 2015/9/23 * Time: 21:20 * 說明: */ @Controller public class Index { @RequestMapping("/hello") @ResponseBody String hello(Model model) { return "hello"; } @RequestMapping("/index") String index(Model model) { return "index"; } }
application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp
執行spring-boot:run(也能夠直接在IDE中執行Application.java main方法)
輸入localhost:8080/index
看到下面可愛的spring的字眼的時候,說明環境搭建沒有問題了,在啓動過程當中出現的log,能夠很好地瞭解到spring boot運行的順序和過程,若是定義log的等級,會有更加詳細的過程,建議初學者設置,如何設置log等級,見系列二
http://my.oschina.net/smartsales/blog/510412
將應用打包成jar並使用一個內嵌HTTP服務器的一個最大好處是,你能夠像其餘方式那樣運行你的應用程序。調試SpringBoot應用也很簡單;你不須要任何特殊IDE或擴展。
打包成jar的形式,目前爲止有個最大的好處是能夠和docker進行完美的配合,一個java -jar 便可運行docker中的spring-boot工程,實現容器的快速部署使用。
若是使用Spring Boot Maven或Gradle插件建立一個可執行jar,你可使用 java -jar 運行你的應用。例如:
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
運行一個打包的程序並開啓遠程調試支持是可能的,這容許你將調試器附加到打包的應用程序上:
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myproject-0.0.1-SNAPSHOT.jar
Spring Boot Maven插件包含一個 run 目標,它能夠用來快速編譯和運行應用程序。應用程序以一種暴露的方式運行,因爲即
時"熱"加載,你能夠編輯資源。
$ mvn spring-boot:run
你可能想使用有用的操做系統環境變量:
$ export MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=128M -Djava.security.egd=file:/dev/./urandom
("egd"設置是經過爲Tomcat提供一個更快的會話keys熵源來加速Tomcat的。)
j360-simplewar
只須要將pom.xml的package改爲war便可
<groupId>me.j360.boot</groupId> <artifactId>j360-simplewar</artifactId> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging>
爲了構建一個便是可執行的,又能部署到一個外部容器的war文件,你須要標記內嵌容器依賴爲"provided",例如:
<?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"> <!-- ... --> <packaging>war</packaging> <!-- ... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- ... --> </dependencies> </project>