一、Spring Boot Starter是什麼?
Spring Boot Starter提供了快速啟動Spring Boot工程的方式,通過對眾多依賴的封裝,開發者無需關心版本兼容性和配置文件,只需一個依賴,即可快速啟動開發。
例如,如果需要使用JPA,只需在maven中添加以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
這樣就可以啟動一個基於Spring Boot和JPA的項目,可以自動配置DataSource、TransactionManager、EntityManager等。
二、Spring Boot Starter分類
Spring Boot Starter有很多種,每一種都封裝了不同的功能,常見幾種Spring Boot Starter如下:
1. spring-boot-starter-web
提供了運行基於web的應用程序所需的所有依賴項。包括Spring MVC、Tomcat等。
2. spring-boot-starter-data-jpa
提供了集成JPA所需的所有依賴項。包括Hibernate、Spring Data JPA、Spring ORM等。
3. spring-boot-starter-test
提供了編寫測試所需的所有依賴項。包括JUnit、Mockito、Hamcrest等。
4. spring-boot-starter-actuator
提供了監視和管理應用程序的依賴項。包括Health、Metrics、Info等。
5. spring-boot-starter-security
提供了安全框架所需的所有依賴項。包括Spring Security、Spring Security OAuth2等。
三、Spring Boot Starter使用示例
下面以spring-boot-starter-data-jpa為例,演示如何使用Spring Boot Starter。
1. 創建Maven項目,在pom.xml中添加如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. 創建一個實體類:
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Integer age;
// getter、setter、toString省略
}
3. 創建一個Repository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
4. 編寫測試用例:
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
UserRepository userRepository;
@Test
void contextLoads() {
User user = new User();
user.setName("Bob");
user.setAge(20);
userRepository.save(user);
System.out.println(user.getId());
}
}
注意:測試用例必須在Spring Boot環境下運行,需要使用@SpringBootTest註解。
四、Spring Boot Starter如何編寫自己的Starter?
以上是對Spring Boot Starter的簡單介紹,如果希望編寫自己的Spring Boot Starter,可以按照以下步驟:
1. 創建一個Maven項目;
2. 在pom.xml中添加以下插件:
<build>
<plugins>
<!-- 將項目打包為jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 將項目打包為Spring Boot Starter -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>starters</id>
<phase>generate-resources</phase>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>starter</classifier>
<includes>
<include>com/example/myapp/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3. 在src/main/java目錄下創建自己的Starter:
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(MyClass.class)
public class MyStarterAutoConfiguration {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
上述代碼中,MyClass是自己編寫的類,MyService和MyServiceImpl是自己編寫的Service介面和實現類。
4. 在src/main/resources/META-INF/spring.factories文件中增加自己的配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.mystarter.MyStarterAutoConfiguration
這樣就完成了自己的Spring Boot Starter編寫,可以通過在項目中添加該Starter的依賴,來使用自己的Starter。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190675.html