Spring Boot Starters简介

在这篇文章中,我们将向你介绍Spring Boot Starters,并将讨论Spring Boot Starters的优点和优势。

简介

在启动任何项目(无论是小型项目还是企业级应用程序)之前,其中关键的方面之一是依赖管理,手动为小型应用程序执行依赖管理并不是一项困难的工作,但对于复杂的应用程序,手动管理所有项目依赖并不理想,容易出现许多问题以及浪费时间,而这些时间可以用于项目的其他一些重要方面。

Spring Boot背后的基本原理之一就是解决类似的问题。Spring Boot Starter是一套方便的依赖描述符,可以很容易地包含在任何级别的应用程序中。这些Starters作为Spring相关技术的引导过程,我们 不再需要担心依赖关系,它们将由Spring Boot Starters自动管理。

Starters包含了许多你需要的依赖项,以使项目快速启动和运行,并且具有一致的、被支持的一组管理传递依赖项。

1. 为什么我们需要Starters?

当我们用Spring Boot开始开发应用时,我们想到的一个基本问题就是为什么我们需要Spring Boot Starters? 或者这些Starters在我的应用中如何帮助到我?

如前所述,这些Starters用于引导应用程序,我们需要的只是在应用程序中包括正确的Starters,而Spring Boot将确保所选Starters所需的所有依赖项都在你的classpath中。

为了更清楚地理解它,我们举一个例子,我们想构建一个简单的Spring Web MVC应用程序,我们需要在开始编写我们的Web应用程序代码之前考虑以下几点。

  • 正确的Spring MVC依赖
  • Web技术所需的依赖(例如,我们想要使用Thymeleaf)
  • 我们需要确保所有这些依赖是兼容的

使用Spring Boot Starters来引导我们的Spring MVC Web应用程序非常简单,我们需要在我们的pom.xml中包含spring-boot-starter-web 这个starter:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

以上pom.xml中的条目将确保所有必需的依赖项都应位于classpath中,因此我们都准备好开始开发web应用程序了。

目前,Spring Boot提供的Starters约有50多个,这还不包括第三方的Starters。有关Starters的更新列表,请参阅Spring Boot Starter

接下来,我将介绍一些常用的Starters。

2. Web Starter

这是最常用的Spring Boot Starter之一,该Starter将确保创建Spring Web应用程序(包括REST)所需的所有依赖包括在你的calsspath中,它还将添加tomcat-starter作为默认服务器来运行我们的Web应用程序。 要在我们的应用程序中包含Web Starter,请在pom.xml中添加以下条目。

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

现在我们可以创建我们的Spring MVC Controller

1
2
3
4
5
6
7
8
@RestController
public class SampleController {
@RequestMapping("/greeting")
String hello() {
return "HelloWorld!";
}
}

如果你运行应用程序并访问http://localhost:8080/greeting,你应该能够获得"Hello Word”作为响应。我们使用最少的代码创建了一个REST控制器。

3. Data JPA Starter

大多数应用程序需要一些持久性机制,而JPA是持久性的标准,Spring Boot Starters带有JPA Starters,你不再需要手动配置这些JPA依赖,而是可以通过在应用程序中添加JPA Starter轻松实现。

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

Spring JPA Starter提供对H2,Derby和Hsqldb的自动支持。让我们看看使用JPA starter创建一个JPA样例应用程序是多么容易。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
protected User() {
}
public User(String firstName, String lastName) {
//this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

如下是我们的UserRepository:

1
2
3
public interface UserRepository extends CrudRepository<User,Long> {
List<User> findUserByLastName(String lastName);
}

接下来我们可以测试我们的代码了,如下是JUnit代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpademoApplicationTests {
@Autowired
UserRepository userRepository;
@Test
public void contextLoads() {
User user = userRepository.save(new User("Demo","User"));
User searchUser= userRepository.findOne(user.getId());
assertNotNull(searchUser);
assertEquals(user.getFirstName(),searchUser.getFirstName());
}
}

正如我们在上面的代码中看到的那样,你不再需要指定那些数据库配置或额外的数据库配置,通过添加JPA starter,我们无需配置或编码即可获得许多开箱即用的功能。

如果需要,你始终可以修改或自定义这些配置。

4. Mail Starter

从应用程序发送电子邮件是非常常见的任务,现在每个应用程序都需要从系统发送电子邮件。Spring Boot Mail starter提供了一种隐藏所有复杂性的简单方法来处理此功能。

我们可以通过在应用程序中添加Mail starter来启用电子邮件支持。

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

我正在使用Mailgun作为我的SMTP服务器,以下是添加到我的application. properties文件中的SMTP详细信息:

1
2
3
4
5
6
spring.mail.host=smtp.mailgun.org
spring.mail.username=postmaster@domain.com
spring.mail.password=mypassword
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.smtp.auth=true

我们的EmailService类负责发送邮件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
public class JavaEmailService {
private JavaMailSender mailSender;
public JavaEmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendEmail(){
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom("noreply@javadevjournal.com");
helper.setTo("xxx@gmail.com");
helper.setSubject("Sample mail subject");
helper.setText("Test Email");
};
mailSender.send(messagePreparator);
}
}

我们使用Spring提供的JavaMailSender来发送电子邮件。 JUnit测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailTest {
@Autowired
JavaEmailService javaEmailService;
@Test
public void sendEmail(){
javaEmailService.sendEmail();
}
}

同样,只需简单的代码和配置即可发送一封简单的电子邮件,Spring Boot Mail Starter确保所有必需的工具已经到位,以快速开始解决实际问题。

请注意,我们在JavaEmailService bean中使用JavaMailSender - 该bean是由Spring Boot自动创建的。

5. Test Starter

我们通常使用Junit、Mockito或Spring Test来测试我们的应用程序。我们可以通过添加Spring Boot Test starter轻松地将所有这些库包含在我们的应用程序中。

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

Spring Boot会自动找到我们正确的版本用于我们的应用程序测试。 这是一个JUnit示例测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailTest {
@Autowired
JavaEmailService javaEmailService;
@Test
public void sendEmail(){
javaEmailService.sendEmail();
}
}

除了这些starter之外,下面还有其他常用的Spring Boot Starter

  • spring-boot-starter-security
  • spring-boot-starter-web-services
  • spring-boot-starter-integration
  • spring-boot-starter-validation
  • spring-boot-starter-actuator

如前所述,请参阅Spring Boot Starter获取Spring Boot提供的Starter的最新列表。

总结

本文提供了一个Spring Boot Starters简介,我们讨论了为什么我们需要这些Starter以及他们如何帮助我们快速引导我们的应用程序。 我们探索了一些最常用的Spring Boot Starter。

建议阅读:
使用Spring Boot构建应用程序

原文链接: https://www.javadevjournal.com/spring/spring-boot-starters/