本文共 5515 字,大约阅读时间需要 18 分钟。
官网:
下载地址:
setting.xml配置文件参考:,可以直接下载【maven-settings.xml 】 也可以直接增加如下代码:< !-- 在mirrors 中配置 mirror -->alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
eclipse中的help—》install new software打开安装页面选择zip包或者在线安装即可
如上图所示,Spring Boot的基础结构共三个文件: - src/main/java 程序开发以及主程序入口 - src/main/resources 配置文件 - src/test/java 测试程序
root package结构:com.sunld.myproject
pom.xml文件中默认有两个模块:
spring-boot-starter:核心模块,包括自动配置支持、日志和YAML; spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito。4.0.0 com.sunld springboot_helloworld 0.0.1-SNAPSHOT jar springboot_helloworld first springboot demo org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
org.springframework.boot spring-boot-starter-web
package com.sunld.helloworld.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;//@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!@RestControllerpublic class HelloWorldController { @RequestMapping("/helloworld") public String helloWorld() { return "HelloWorld ...."; }}
把项目通过maven命令 mvn package 打成jar包,通过命令 java -jar springboot_helloworld-0.0.1-SNAPSHOT.jar 直接执行jar包
借助maven插件,执行mvn spring-boot:run 即可运行项目。
启动效果如下
package com.sunld.helloworld.controller;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;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 org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;@RunWith(SpringRunner.class)@SpringBootTest/** * * @author sunliaodong * 使用mockmvc进行,利用MockMvcResultHandlers.print()打印出执行结果。 * 使用MockServletContext来构建一个空的WebApplicationContext, * 这样我们创建的HelloController就可以在@Before函数中创建并传递到MockMvcBuilders.standaloneSetup()函数中。 */public class HelloWorldControllerTest { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/helloworld").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().string("HelloWorld ....")) .andDo(MockMvcResultHandlers.print()) .andReturn(); }}
深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
org.springframework.boot spring-boot-devtools true
org.springframework.boot spring-boot-maven-plugin true
该模块在完整的打包环境下运行的时候会被禁用。如果你使用java -jar启动应用或者用一个特定的classloader启动,它会认为这是一个“生产环境”。
目前已经完成了springboot的基本使用,通过helloworld来了解springboot配置方便性和易用性。
完整代码: