深入浅析 Spring Boot Starter
SpringBoot简介
Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了SpringBoot框架,它的作用很简单,就是帮我们自动配置。SpringBoot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,SpringBoot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
依赖管理是任何复杂项目的关键部分。以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少。
SpringBootstarter就是为了解决这个问题而诞生的。StarterPOM是一组方便的依赖描述符,您可以将其包含在应用程序中。您可以获得所需的所有Spring和相关技术的一站式服务,无需通过示例代码搜索和复制粘贴依赖。
我们有超过30个Bootstarter—下文将提到其中一部分。
2、WebStarter
首先,让我们来看看REST服务开发。我们可以使用像SpringMVC、Tomcat和Jackson这样的库,这对于单个应用程序来说是还是存在许多依赖。
SpringBootstarter通过添加一个依赖来帮助减少手动添加依赖的数量。因此,不要手动指定依赖,您只需要添加一个starter即可,如下所示:
org.springframework.boot spring-boot-starter-web
现在我们可以创建一个REST控制器。为了简单起见,我们不会使用数据库,只专注于REST控制器:
@RestController publicclassGenericEntityController{ privateListentityList=newArrayList<>(); @RequestMapping("/entity/all") publicList findAll(){ returnentityList; } @RequestMapping(value="/entity",method=RequestMethod.POST) publicGenericEntityaddEntity(GenericEntityentity){ entityList.add(entity); returnentity; } @RequestMapping("/entity/findby/{id}") publicGenericEntityfindById(@PathVariableLongid){ returnentityList.stream(). filter(entity->entity.getId().equals(id)). findFirst().get(); } }
GenericEntity是一个简单的bean,id的类型为Long,value为String类型。
就是这样,应用程序可以开始运行了,您可以访问http://localhost:8080/springbootapp/entity/all并检查控制器是否正常工作。
我们已经创建了一个配置非常少的REST应用程序。
3、TestStarter
对于测试,我们通常使用以下组合:SpringTest、JUnit、Hamcrest和Mockito。我们可以手动包含所有这些库,但使用以下SpringBootstarter方式可以自动包含这些库:
org.springframework.boot spring-boot-starter-test test
请注意,您不需要指定工件的版本号。SpringBoot会自动选择合适的版本—您仅需要指定spring-boot-starter-parent-artifact的版本。如果之后您想要升级Boot库和依赖,只需在一个地方升级Boot版本即可,它将会处理其余部分。
让我们来测试一下之前创建的控制器。
测试控制器有两种方法:
- 使用mock环境
- 使用嵌入式Servlet容器(如Tomcat或Jetty)
在本例中,我们将使用一个mock环境:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) @WebAppConfiguration publicclassSpringBootApplicationTest{ @Autowired privateWebApplicationContextwebApplicationContext; privateMockMvcmockMvc; @Before publicvoidsetupMockMvc(){ mockMvc=MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test publicvoidgivenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throwsException{ MediaTypecontentType=newMediaType(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是一个Hamcrestmatcher,@Before是一个JUnit注解。这些都可以通过导入这一个这样的starter依赖来引入。
4、DataJPAStarter
大多数Web应用程序都存在某些持久化—常见的是JPA。
让我们使用starter来开始,而不是手动定义所有关联的依赖:
org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime
请注意,我们对这些数据库已经有了开箱即用的自动支持:H2、Derby和Hsqldb。在我们的示例中,我们将使用H2。
现在让我们为实体创建仓储(repository):
publicinterfaceGenericEntityRepositoryextendsJpaRepository{}
现在是测试代码的时候了。这是JUnit测试:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) publicclassSpringBootJPATest{ @Autowired privateGenericEntityRepositorygenericEntityRepository; @Test publicvoidgivenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK(){ GenericEntitygenericEntity= genericEntityRepository.save(newGenericEntity("test")); GenericEntityfoundedEntity= genericEntityRepository.findOne(genericEntity.getId()); assertNotNull(foundedEntity); assertEquals(genericEntity.getValue(),foundedEntity.getValue()); } }
我们没有花时间指定数据库厂商、URL连接和凭据。没有额外所需的配置,这些都受益于Boot的默认支持。但是,如果您需要,可以进行详细配置。
5、MailStarter
企业开发中一个非常常见的任务就是发送电子邮件,直接使用JavaMailAPI来处理通常很困难。
SpringBootstarter屏蔽了这些复杂性—mail依赖可以通过以下方式指定:
org.springframework.boot spring-boot-starter-mail
现在我们可以直接使用JavaMailSender。让我们开始编写一些测试。
为了测试,我们需要一个简单的SMTP服务器。在此例中,我们将使用Wiser。将其包含到我们的POM中:
org.subethamail subethasmtp 3.1.7 test
最新版本的Wiser可以在Maven中央仓库(http://search.maven.org/#search%7Cga%7C1%7Csubethasmtp)中找到。
以下是测试源码:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) publicclassSpringBootMailTest{ @Autowired privateJavaMailSenderjavaMailSender; privateWiserwiser; privateStringuserTo="user2@localhost"; privateStringuserFrom="user1@localhost"; privateStringsubject="Testsubject"; privateStringtextMail="Textsubjectmail"; @Before publicvoidsetUp()throwsException{ finalintTEST_PORT=25; wiser=newWiser(TEST_PORT); wiser.start(); } @After publicvoidtearDown()throwsException{ wiser.stop(); } @Test publicvoidgivenMail_whenSendAndReceived_thenCorrect()throwsException{ SimpleMailMessagemessage=composeEmailMessage(); javaMailSender.send(message); Listmessages=wiser.getMessages(); assertThat(messages,hasSize(1)); WiserMessagewiserMessage=messages.get(0); assertEquals(userFrom,wiserMessage.getEnvelopeSender()); assertEquals(userTo,wiserMessage.getEnvelopeReceiver()); assertEquals(subject,getSubject(wiserMessage)); assertEquals(textMail,getMessage(wiserMessage)); } privateStringgetMessage(WiserMessagewiserMessage) throwsMessagingException,IOException{ returnwiserMessage.getMimeMessage().getContent().toString().trim(); } privateStringgetSubject(WiserMessagewiserMessage)throwsMessagingException{ returnwiserMessage.getMimeMessage().getSubject(); } privateSimpleMailMessagecomposeEmailMessage(){ SimpleMailMessagemailMessage=newSimpleMailMessage(); mailMessage.setTo(userTo); mailMessage.setReplyTo(userFrom); mailMessage.setFrom(userFrom); mailMessage.setSubject(subject); mailMessage.setText(textMail); returnmailMessage; } }
在测试中,@Before和@After方法负责启动和停止邮件服务器。
请注意,我们装配了JavaMailSenderbean—该bean是由SpringBoot自动创建。
与Boot中的其他默认值一样,JavaMailSender的email设置可以在application.properties中自定义:
spring.mail.host=localhost spring.mail.port=25 spring.mail.properties.mail.smtp.auth=false
我们在localhost:25上配置了邮件服务器,不需要身份验证。
6、结论
在本文中,我们介绍了Starter,解释了为什么我们需要它们,并提供了如何在项目中使用它们的示例。
让我们回顾一下使用SpringBootstarter的好处:
- 增加pom可管理性
- 生产就绪、测试与依赖配置支持
- 减少项目的整体配置时间
总结
以上所述是小编给大家介绍的SpringBootStarter的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!