您的位置:首页 > 编程语言 > Java开发

深入浅析 Spring Boot Starter

2017-10-13 09:06 801 查看

Spring Boot 简介

Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。

依赖管理是任何复杂项目的关键部分。以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少。

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 环境
  • 使用嵌入式 Servlet 容器(如 Tomcat 或 Jetty)

在本例中,我们将使用一个 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.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

请注意,我们对这些数据库已经有了开箱即用的自动支持:H2、Derby 和 Hsqldb。在我们的示例中,我们将使用 H2。

现在让我们为实体创建仓储(repository):

public interface GenericEntityRepositoryextends JpaRepository<GenericEntity,Long>{}

现在是测试代码的时候了。这是 JUnit 测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class SpringBootJPATest{
@Autowired
private GenericEntityRepository genericEntityRepository;
@Test
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK(){
GenericEntity genericEntity =
genericEntityRepository.save(new GenericEntity("test"));
GenericEntity foundedEntity =
genericEntityRepository.findOne(genericEntity.getId());
assertNotNull(foundedEntity);
assertEquals(genericEntity.getValue(), foundedEntity.getValue());
}
}

我们没有花时间指定数据库厂商、URL 连接和凭据。没有额外所需的配置,这些都受益于 Boot 的默认支持。 但是,如果您需要,可以进行详细配置。

5、Mail Starter

企业开发中一个非常常见的任务就是发送电子邮件,直接使用 Java Mail API 来处理通常很困难。

Spring Boot starter 屏蔽了这些复杂性 ― mail 依赖可以通过以下方式指定:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

现在我们可以直接使用 JavaMailSender 。让我们开始编写一些测试。

为了测试,我们需要一个简单的 SMTP 服务器。在此例中,我们将使用 Wiser。将其包含到我们的 POM 中:

<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
<scope>test</scope>
</dependency>

最新版本的 Wiser 可以在 Maven 中央仓库 ( http://search.maven.org/#search%7Cga%7C1%7Csubethasmtp)中找到。

以下是测试源码:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class SpringBootMailTest{
@Autowired
private JavaMailSender javaMailSender;
private Wiser wiser;
private String userTo = "user2@localhost";
private String userFrom = "user1@localhost";
private String subject = "Test subject";
private String textMail = "Text subject mail";
@Before
public void setUp()throws Exception {
final int TEST_PORT = 25;
wiser = new Wiser(TEST_PORT);
wiser.start();
}
@After
public void tearDown()throws Exception {
wiser.stop();
}
@Test
public void givenMail_whenSendAndReceived_thenCorrect()throws Exception {
SimpleMailMessage message = composeEmailMessage();
javaMailSender.send(message);
List<WiserMessage> messages = wiser.getMessages();
assertThat(messages, hasSize(1));
WiserMessage wiserMessage = messages.get(0);
assertEquals(userFrom, wiserMessage.getEnvelopeSender());
assertEquals(userTo, wiserMessage.getEnvelopeReceiver());
assertEquals(subject, getSubject(wiserMessage));
assertEquals(textMail, getMessage(wiserMessage));
}
private String getMessage(WiserMessage wiserMessage)
throws MessagingException, IOException {
return wiserMessage.getMimeMessage().getContent().toString().trim();
}
private String getSubject(WiserMessage wiserMessage)throws MessagingException {
return wiserMessage.getMimeMessage().getSubject();
}
private SimpleMailMessage composeEmailMessage(){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(userTo);
mailMessage.setReplyTo(userFrom);
mailMessage.setFrom(userFrom);
mailMessage.setSubject(subject);
mailMessage.setText(textMail);
return mailMessage;
}
}

在测试中, @Before 和 @After 方法负责启动和停止邮件服务器。

请注意,我们装配了 JavaMailSender bean ― 该 bean 是 由 Spring Boot 自动创建 。

与 Boot 中的其他默认值一样, JavaMailSender 的 email 设置可以在 application.properties 中自定义:

spring.mail.host=localhost
spring.mail.port=25
spring.mail.properties.mail.smtp.auth=false

我们在 localhost:25 上配置了邮件服务器,不需要身份验证。

6、结论

在本文中,我们介绍了 Starter,解释了为什么我们需要它们,并提供了如何在项目中使用它们的示例。

让我们回顾一下使用 Spring Boot starter 的好处:

  • 增加 pom 可管理性
  • 生产就绪、测试与依赖配置支持
  • 减少项目的整体配置时间

总结

以上所述是小编给大家介绍的Spring Boot Starter的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring boot starter