天天看點

Spring Boot系列-- 如何自定義Starter!

建立一個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

Spring Boot系列-- 如何自定義Starter!

localhost:8080/hobby

Spring Boot系列-- 如何自定義Starter!

由于沒有在application.properties中添加指定屬性,是以會使用預設的屬性。

接下來在application.properties添加我們自己的屬性

com.wisdom.hobby=football
com.wisdom.name=messi           

複制

重新開機springboot項目,繼續通路

Spring Boot系列-- 如何自定義Starter!
Spring Boot系列-- 如何自定義Starter!

blog.csdn.net/qq_21150865/article/details/83504780