1、概述
依赖打点是任何巨大项目标要害部门。以手动的方法来实现依赖打点不太现实,你得花更多时间,同时你在项目标其他重要方面能支付的时间就会变得越少。
Spring Boot starter 就是为了办理这个问题而降生的。Starter POM 是一组利便的依赖描写符,您可以将其包括在应用措施中。您可以得到所需的所有 Spring 和相关技能的一站式处事,无需通过示例代码搜索和复制粘贴依赖。
我们有高出 30 个 Boot starter — 下文将提到个中一部门。
2、Web Starter
首先,让我们来看看 REST 处事开拓。我们可以利用像 Spring MVC、Tomcat 和 Jackson 这样的库,这对付单个应用措施来说是照旧存在很多依赖。
Spring Boot starter 通过添加一个依赖来辅佐淘汰手动添加依赖的数量。 因此,软件开发,不要手动指定依赖,您只需要添加一个 starter 即可,如下所示:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
此刻我们可以建设一个 REST 节制器。为了简朴起见,我们不会利用数据库,软件开发,只专注于 REST 节制器:
@RestController
public class GenericEntityController {
private List<GenericEntity> entityList = new ArrayList<>();
@RequestMapping("/entity/all")
public List<GenericEntity> findAll() {
return entityList;
}
@RequestMapping(value = "/entity", method = RequestMethod.POST)
public GenericEntity addEntity(GenericEntity entity) {
entityList.add(entity);
return entity;
}
@RequestMapping("/entity/findby/{id}")
public GenericEntity findById(@PathVariable Long id) {
return entityList.stream().
filter(entity -> entity.getId().equals(id)).
findFirst().get();
}
}
GenericEntity 是一个简朴的 bean,id 的范例为 Long,value 为 String 范例。
就是这样,应用措施可以开始运行了,您可以会见 http://localhost:8080/springbootapp/entity/all 并查抄节制器是否正常事情。
我们已经建设了一个设置很是少的 REST 应用措施。
3、Test Starter
对付测试,我们凡是利用以下组合:Spring Test、JUnit、Hamcrest 和 Mockito。我们可以手动包括所有这些库,但利用以下 Spring Boot starter 方法可以自动包括这些库:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
请留意,您不需要指定工件的版本号。Spring Boot 会自动选择符合的版本 — 您仅需要指定 spring-boot-starter-parent-artifact 的版本。 假如之后您想要进级 Boot 库和依赖,只需在一个处所进级 Boot 版本即可,它将会处理惩罚其余部门。
让我们来测试一下之前建设的节制器。
测试节制器有两种要领:
在本例中,我们将利用一个 mock 情况:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class SpringBootApplicationTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect()
throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).
andExpect(MockMvcResultMatchers.status().isOk()).
andExpect(MockMvcResultMatchers.content().contentType(contentType)).
andExpect(jsonPath("$", hasSize(4)));
}
}
这里重要的是 @WebAppConfiguration 注解和 MockMVC 是 spring-test 模块的一部门,hasSize 是一个 Hamcrest matcher,@Before 是一个 JUnit 注解。这些都可以通过导入这一个这样的 starter 依赖来引入。
4、Data JPA Starter
大大都 Web 应用措施都存在某些耐久化 — 常见的是 JPA。
让我们利用 starter 来开始,而不是手动界说所有关联的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.pdatabase</groupId>
<artifactId>p</artifactId>
<scope>runtime</scope>
</dependency>
请留意,我们对这些数据库已经有了开箱即用的自动支持:p、Derby 和 Hsqldb。在我们的示例中,软件开发,我们将利用 p。
此刻让我们为实体建设仓储(repository):
public interface GenericEntityRepository extends JpaRepository<GenericEntity, Long> {}