注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不一样可能会有细微差异。
因为 J2EE 的开发变得笨重,繁多的配置,错乱的依赖管理,低下的开发效率,复杂的部署流程,第三方技术的集成难度较大等。同时随着复杂项目的演进,微服务分布式架构思想逐渐进入开发者的视野。java
Spring Boot
提供了一组工具只须要极少的配置就能够快速的构建并启动基于 Spring 的应用程序。解决了传统 Spring 开发须要配置大量配置文件的痛点,同时 Spring Boot
对于第三方库设置了合理的默认值,能够快速的构建起应用程序。固然 Spring Boot
也能够轻松的自定义各类配置,不管是在开发的初始阶段仍是投入生成的后期阶段。git
<!-- more -->github
说了那么多的 Spring Boot 的好处,那么使用 Spring Boot 须要哪些前置知识呢?我简单列举了一下。web
如今咱们已经了解了 Spring Boot 是什么,下面咱们将使用 Spring Boot 开发一个入门案例,来体验 Spring Boot 开发姿式是如何的优雅与迅速。
Spring Boot 官方已经为咱们如何快速启动 Spring Boot 应用程序提供了多种方式。面试
你能够在 Spring 官方网站直接生成项目下载导入IDE进行开发。spring
https://start.spring.io/
也能够直接克隆 GitHub 上的初始项目进行体验。shell
git clone https://github.com/spring-guides/gs-spring-boot.git cd gs-spring-boot/initial
这里咱们选择后者,直接克隆进入到 initial 文件夹使用 maven 进行编译启动。springboot
mvn package && java -jar target/gs-spring-boot-0.1.0.jar
第一次编译须要下载须要的依赖,耗时会比较长,编译完成以后紧接着能够看到 Spring 的启动标志。这时 Spring Boot 的 web程序已经运行在8080端口了。服务器
$ curl -s localhost:8080 Greetings from Spring Boot!
下面手动编写一个 Spring Boot 入门案例,快速的开发一个 web mvc 应用。
项目结构以下:架构
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
spring-boot-starter-parent
是Spring Boot 的核心依赖,它里面定义了各类在开发中会用到的第三方 jar 的版本信息,所以咱们在引入其余的 Spring Boot 为咱们封装的启动器的时候都不在须要指定版本信息。若是咱们须要自定义版本信息,能够直接覆盖版本属性值便可。
spring-boot-starter-web
提供 web 以及 MVC 和 validator 等web开发框架的支持。
spring-boot-starter-test
提供测试模块的支持。如 Junit,Mockito。
须要说明的是,Spring Boot 为咱们提供了不少的已经封装好的称为启动器(starter)的依赖项。让咱们在使用的时候不须要再进行复杂的配置就能够迅速的进行应用集成。全部的官方启动器依赖能够在这里查看。
全部 官方发布的启动器都遵循相似的命名模式;spring-boot-starter-*
,这里*
是指特定类型的应用程序。此命名结构旨在帮助您寻找启动器。注意:编写本身的启动器的时候不该该使用这种命名方式。
@SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { // 开始检查spring boot 提供的 beans System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; } }
@SpringBootApplication
注解是一个便利的注解,它包含了如下几个注解。
@Configuration
定义配置类。@EnableAutoConfiguration
开启自动配置。@EnableWebMvc
标记为 web应用程序。@ComponentScan
组件扫描。@RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
@RestController
是 @Controller
与 @ResponseBody
的结合体。
直接启动 HelloApplication.java
类就能够在控制台看到启动输出,而后访问8080端口查看启动是否正常。
通过上面的例子,已经使用 Spring Boot 快速的建立了一个 web 应用并进行了简单的访问测试。
结合上面提到的 Spring Boot 启动器知识,Spring Boot 已经为咱们提供了丰富的第三方框架,测试框架也不例外。
导入单元测试依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
编写单元测试
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * 单元测试 */ @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloApplicationTests { @Autowired private MockMvc mvc; @Test public void contextLoads() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string("Greetings from Spring Boot!")); } }
关于上面代码的一些说明。
运行没有任何异常说明程序测试经过。
import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import java.net.URL; /** * <p> * 嵌入式服务器由随机端口启动webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT * 而且在运行时发现实际端口@LocalServerPort * * @Author niujinpeng * @Date 2018/12/4 15:02 */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloApplicationTestBySpringBoot { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void setup() throws Exception { this.base = new URL("http://localhost:" + port + "/"); } @Test public void getHello() throws Exception { ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); assert (response.getBody().equals("Greetings from Spring Boot!")); } }
嵌入式服务器由随机端口启动 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
而且在运行时使用注解 @LocalServerPort
发现实际端口。
运行测试类经过输出。
2018-12-06 22:28:01.914 INFO 14320 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2018-12-06 22:28:01.914 INFO 14320 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2018-12-06 22:28:01.937 INFO 14320 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 23 ms
文章代码已经上传到 GitHub Spring Boot 入门案例。
<完>
我的网站:https://www.codingme.net
若是你喜欢这篇文章,能够关注公众号,一块儿成长。
关注公众号回复资源能够没有套路的获取全网最火的的 Java 核心知识整理&面试资料。