使用springboot自定義starter
阿新 • • 發佈:2018-12-18
- 建立一個maven專案,在pom檔案中新增如下依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wisdom</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>1.0-REALEASE</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring-boot-autoconfigure
此jar包中包含大量核心註解,包含條件註解等。
- 建立properties屬性類,用於讀取屬性。
@ConfigurationProperties(prefix = "com.wisdom") public class HelloServiceProperties { private String name = "wisdom"; private String hobby = "basketball"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }
@ConfigurationProperties
配置此註解可以自動匯入application.properties配置檔案中的屬性,前提需要指定屬性字首prefix。如果application.properties檔案中未指定相應屬性,便使用預設的,如上name=“wisdom”,hobby=“basketball”.
- 建立配置類
public class HelloServiceConfiguration { private String name; private String hobby; public String getName() { return "name is " + name; } public String getHobby() { return "hobby is " + hobby; } public void setName(String name) { this.name = name; } public void setHobby(String hobby) { this.hobby = hobby; } }
- 建立自動配置類:
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.wisdom", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties;
@Bean
@ConditionalOnMissingBean(HelloServiceConfiguration.class)
public HelloServiceConfiguration helloServiceConfiguration() {
HelloServiceConfiguration helloService = new HelloServiceConfiguration();
helloService.setName(helloServiceProperties.getName());
helloService.setHobby(helloServiceProperties.getHobby());
return helloService;
}
}
@Configuration
:表明此類是一個配置類,將變為一個bean被spring進行管理。@EnableConfigurationProperties
:啟用屬性配置,將讀取HelloServiceProperties裡面的屬性。@ConditionalOnClass
:當類路徑下面有HelloServiceConfiguration此類時,自動配置。@ConditionalOnProperty
:判斷指定的屬性是否具備指定的值。@ConditionalOnMissingBean
:當容器中沒有指定bean是,建立此bean。
- 在resources資料夾下面新建一個META-INF檔案,並在下面建立spring.factories檔案,將4中的配置類進行註冊。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wisdom.HelloServiceAutoConfiguration
至此,自定義的spring-boot-starter-hello
編寫完畢,當然springboot官方建議對於非官方的starter命名方式為xxx-spring-boot-starter
。執行mvn clean install
將專案打成一個jar包。
- 新建一個springboot專案,在pom檔案中新增剛剛打包的jar的座標。
<dependency>
<groupId>com.wisdom</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-REALEASE</version>
</dependency>
在啟動類上編寫訪問介面
@SpringBootApplication
@RestController
public class Springboot03Application {
@Autowired
private HelloServiceConfiguration helloService;
public static void main(String[] args) {
SpringApplication.run(Springboot03Application.class, args);
}
@RequestMapping("/name")
public String getName() {
return helloService.getName();
}
@RequestMapping("/hobby")
public String getHobby() {
return helloService.getHobby();
}
}
啟動此springboot專案,依次訪問 localhost:8080/name 和localhost:8080/hobby。 由於沒有在application.properties中新增指定屬性,所以會使用預設的屬性。
接下來在application.properties新增我們自己的屬性
com.wisdom.hobby=football
com.wisdom.name=messi
重啟springboot專案,繼續訪問