欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot創(chuàng)建自定義starter詳解

 更新時(shí)間:2024年01月22日 11:02:46   作者:岸河  
這篇文章主要介紹了SpringBoot創(chuàng)建自定義starter詳解,Starter是Spring Boot中的一個(gè)非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴(lài)整合起來(lái)并對(duì)模塊內(nèi)的Bean根據(jù)環(huán)境(條件)進(jìn)行自動(dòng)配置,需要的朋友可以參考下

自定義starter

背景

(1)有時(shí)在公司內(nèi)部有時(shí)候自研中間件,需要被其他項(xiàng)目依賴(lài),這個(gè)時(shí)候直接引入starter,可以實(shí)現(xiàn)自動(dòng)化配置,可以做到對(duì)其他項(xiàng)目代碼無(wú)污染入侵。

(2)Spring Boot大量使用了starter模式,比如spring-boot-starter-redis,spring-boot-starter-jdbc,spring-boot-starter-data-jpa,spring-boot-starter-amqp,我們自己做一遍,了然于心,更能夠加深對(duì)Spring Boot的編程思想的理解與學(xué)習(xí)。

1.首先我們使用IDEA的Spring Initializr直接創(chuàng)建工程,引入web依賴(lài)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.然后自帶的刪掉啟動(dòng)類(lèi),測(cè)試類(lèi),下面的maven插件。我們創(chuàng)建的starter屬于依賴(lài)包,不需要啟動(dòng)類(lèi)。否則在后續(xù)打jar包的時(shí)候會(huì)報(bào)錯(cuò)

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

3.創(chuàng)建配置類(lèi),可以為引入的starter配置相關(guān)屬性

@ConfigurationProperties(prefix = "spring.user")
public class UserProperties {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

4.創(chuàng)建Client端,定義相關(guān)方法操控屬性,模擬進(jìn)行數(shù)據(jù)處理。

public class UserClient {
    private UserProperties userProperties;
    public UserClient() {
    }
    public UserClient(UserProperties userProperties) {
        this.userProperties = userProperties;
    }
    public String getName(){
        return userProperties.getName();
    }
    public Integer getAge(){
        return userProperties.getAge();
    }
}

5.自動(dòng)創(chuàng)建客戶(hù)端,創(chuàng)建UserClient實(shí)例

注意:@ConditionalOnProperty是用來(lái)控制是否啟用配置,它的name或者value屬性意義相同,區(qū)別是name是數(shù)組,value只綁定一個(gè)屬性。當(dāng)name或者value的屬性為havingValue指定值的時(shí)候配置才會(huì)生效,否則配置不生效。即在下面的配置中,只有當(dāng)enabled為true的時(shí)候配置才生效。

@Configuration
@EnableConfigurationProperties(value = UserProperties.class)
public class UserAutoConfigure {
    @Bean
    @ConditionalOnProperty(prefix = "spring.user", value = "enabled", havingValue = "true")
    public UserClient userClient(UserProperties userProperties) {
        return new UserClient(userProperties);
    }
}

6.激活配置類(lèi)。在別的項(xiàng)目中如果想激活配置類(lèi)有兩種辦法,一個(gè)是通過(guò)spring.factories文件,一個(gè)是通過(guò)注解實(shí)現(xiàn),下面來(lái)分別實(shí)現(xiàn)這兩種。

通過(guò)spring.factories文件

在resources下創(chuàng)建META-INF目錄,在其下面創(chuàng)建spring.factories文件,如下聲明自己自動(dòng)配置類(lèi)UserAutoConfigure的路徑

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.anhe.springbootstarteruser.properties.UserAutoConfigure

或者通過(guò)注解實(shí)現(xiàn),只需要在主類(lèi)上添加@EnableUserClient注解,不需要編寫(xiě)spring.factories文件,就可以激活配置類(lèi),注解定義如下:

關(guān)鍵在于@Import注解,他也申明了自動(dòng)配置類(lèi)UserAutoConfigure的路徑

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({UserAutoConfigure.class})
public @interface EnableUserClient {
}

7.配置application.properties提示信息,我們更傾向于在添加配置的時(shí)候能夠看到配置信息或者默認(rèn)值之類(lèi)的,可以按如下方法配置:

在META-INF下創(chuàng)建文件spring-configuration-metadata.json,內(nèi)容如下:

{
  "properties": [
    {
      "name": "spring.user.name",
      "defaultValue": "cxytinadi"
    },
    {
      "name": "spring.user.enabled",
      "type": "java.lang.Boolean",
      "defaultValue": false
    },
    {
      "name": "spring.user.age",
      "type": "java.lang.Integer",
      "defaultValue": 18
    }
  ]
}

然后在其他項(xiàng)目配置的時(shí)候就會(huì)自動(dòng)出現(xiàn)提示,包括配置的默認(rèn)值。

8.然后在Terminal輸入:

mvn clean install

相關(guān)jar包就會(huì)打包到本地倉(cāng)庫(kù),本地其他項(xiàng)目就可以直接使用。

9.demo示例:

其他項(xiàng)目引入依賴(lài):

<dependency>
     <groupId>com.anhe</groupId>
     <artifactId>spring-boot-starter-user</artifactId>
     <version>0.0.1-SNAPSHOT</version>
</dependency>

application.properties加入配置

spring.user.age=20
spring.user.name=tystsyts
spring.user.enabled=true

如果直接是配置注解啟動(dòng),需要在主類(lèi)上加上@EnableUserClient注解,然后在新項(xiàng)目編寫(xiě)controller驗(yàn)證:

@RestController
public class UserController {
    @Autowired
    private UserClient userClient;
    @GetMapping("/user")
    public Map<String,Object> getUserInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("name",userClient.getName());
        map.put("age",userClient.getAge());
        return map;
    }
}

瀏覽器訪問(wèn)//localhost:8080/user就可以獲取到結(jié)果

{
  "name": "tystsyts",
  "age": 20
}

到此這篇關(guān)于SpringBoot創(chuàng)建自定義starter詳解的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論