坑了。。。
这篇blog已经不打算填坑了,一来以后不太会用spring-boot,二来坑填的也没啥意义,所以就这样吧。
Overview
搞Java那还是在5年前上大学的时候,那个时候spring印象中还是2.x,那个时候用spring还要写一大堆的xml配置,简直蛋疼无比。用Java做web开发也是各种蛋疼,还有各种框架,除了spring还有structs/hibernate/ibatis等等。
因为团队都是做Java的,而且前段时间也接手了几个SpringMVC的模块,才知道Spring已经进入了4.x的时代了,而且也有了很大改变,更适合于现代互联网web开发了,也不用写什么xml了。所以准备找时间重新学习一下spring4,而且spring.io新推出了一个叫做spring-boot的“微框架”,更适合于做快速web开发。
基本
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>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (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>
尽管spring-boot还支持除了maven以外的一些构建工具,但是然并卵,生产管理还是用maven的居多,graddle有空可以看看。如果不想用<parent>
引入相关依赖,也可以用以下配置:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
annotation
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
在Java1.6时代,annotation其实并没有多大用处,但是在spring-boot中,几乎到处都是annotation。annotation本身有不少好处,可以增强代码复用,并且可以让代码可读性更好。
@RestController
和@RequestMapping
前者用于标注一个类是controller类,可以辅助生成一些功能。而后者是用来标注rest对应的path的。
spring-boot的一些模块
| name | 描述 |
| spring-boot-starter | 主要包,包括auto-configuration支持、logging、YAML等 |
| spring-boot-starter-actuator | 应用监控管理 |
| spring-boot-starter-amqp | Support for the “Advanced Message Queuing Protocol” via spring-rabbit. |
| spring-boot-starter-aop | Support for aspect-oriented programming including spring-aop and AspectJ. |
| spring-boot-starter-artemis | Support for “Java Message Service API” via Apache Artemis. |
| spring-boot-starter-batch | Support for “Spring Batch” including HSQLDB database. |
| spring-boot-starter-cache | Cache的抽象 |
| spring-boot-starter-cloud-connectors | Support for “Spring Cloud Connectors” which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku. |
| spring-boot-starter-data-elasticsearch | Support for the Elasticsearch search and analytics engine including spring-data-elasticsearch. |
| spring-boot-starter-data-gemfire | Support for the GemFire distributed data store including spring-data-gemfire. |
| spring-boot-starter-data-jpa | Support for the “Java Persistence API” including spring-data-jpa, spring-orm and Hibernate. |
| spring-boot-starter-data-mongodb | Support for the MongoDB NoSQL Database, including spring-data-mongodb. |
| spring-boot-starter-data-redis | Support for the REDIS key-value data store, including spring-data-redis. |
| spring-boot-starter-data-rest | Support for exposing Spring Data repositories over REST via spring-data-rest-webmvc. |
| spring-boot-starter-data-solr | Support for the Apache Solr search platform, including spring-data-solr. |
| spring-boot-starter-freemarker | Support for the FreeMarker templating engine. |
| spring-boot-starter-groovy-templates | Support for the Groovy templating engine. |
| spring-boot-starter-hateoas | Support for HATEOAS-based RESTful services via spring-hateoas. |
| spring-boot-starter-hornetq | Support for “Java Message Service API” via HornetQ. |
| spring-boot-starter-integration | Support for common spring-integration modules. |
| spring-boot-starter-jdbc | Support for JDBC databases. |
| spring-boot-starter-jersey | Support for the Jersey RESTful Web Services framework. |
| spring-boot-starter-jta-atomikos | Support for JTA distributed transactions via Atomikos. |
| spring-boot-starter-jta-bitronix | Support for JTA distributed transactions via Bitronix. |
| spring-boot-starter-mail | Support for javax.mail. |
| spring-boot-starter-mobile | Support for spring-mobile. |
| spring-boot-starter-mustache | Support for the Mustache templating engine. |
| spring-boot-starter-security | Support for spring-security. |
| spring-boot-starter-social-facebook | Support for spring-social-facebook. |
| spring-boot-starter-social-linkedin | Support for spring-social-linkedin. |
| spring-boot-starter-social-twitter | Support for spring-social-twitter. |
| spring-boot-starter-test | Support for common test dependencies, including JUnit, Hamcrest and Mockito along with the spring-test module. |
| spring-boot-starter-thymeleaf | Support for the Thymeleaf templating engine, including integration with Spring. |
| spring-boot-starter-velocity | Support for the Velocity templating engine. |
| spring-boot-starter-web | Support for full-stack web development, including Tomcat and spring-webmvc. |
| spring-boot-starter-websocket | Support for WebSocket development. |
| spring-boot-starter-ws | Support for Spring Web Services. |