日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

springBoot自定義場景啟動(dòng)器starter

作者:IT大兵 更新時(shí)間: 2022-02-17 編程語言

目前創(chuàng)建一個(gè)自定義啟動(dòng)器:然后再SpringBoot中可以引入該依賴包,完成對(duì)該功能快速配置;

1.示意圖:?

?

2.實(shí)現(xiàn)步驟:

1.創(chuàng)建一個(gè)MAVEN項(xiàng)目,它定位于一個(gè)場景啟動(dòng)器:hello-spring-boot-starter,這個(gè)MAVEN項(xiàng)目只需要引入自定義的autoconfigurer依賴,其他src、resource都可以不用要;

<!-- 啟動(dòng)器 -->
    <dependencies>
        <!-- 引入自動(dòng)配置模塊 -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

2. 創(chuàng)建一個(gè)MAVEN項(xiàng)目,定義一個(gè)hello-spring-boot-starter-autoconfigurer,并引入spring-boot-starter依賴;

    <dependencies>
        <!-- 引入spring-boot-starter,所有starter的基本配合 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <dependency>
            <groupId> org.springframework.boot </groupId>
            <artifactId> spring-boot-configuration-processor </artifactId>
            <optional> true </optional>
        </dependency>
    </dependencies>

2.1定義一個(gè)屬性類:

package bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * @program: hello-spring-boot-starter
 * @description:
 * @author: zdb
 * @motto: 認(rèn)真寫好每一行代碼
 * @create: 2021-09-11 16:25
 */
//定義配置文件中屬性的前綴
@ConfigurationProperties(prefix = "zdb.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;
    public String getPrefix() {
        return prefix;
    }
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
    public String getSuffix() {
        return suffix;
    }
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

2.2定義一個(gè)service類,該類默認(rèn)不放入IOC容器中,需要通過配置類完成對(duì)他的注入;

package service;
import bean.HelloProperties;
/**
 * @program: hello-spring-boot-starter
 * @description: 這個(gè)類通過HelloServiceAutoConfiguration實(shí)現(xiàn)注入到IOC容器中;
 * @author: zdb
 * @motto: 認(rèn)真寫好每一行代碼
 * @create: 2021-09-11 16:55
 */
public class HelloService {
    /**
     * 這里采用set方式注入屬性;
     */
    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();
    }
}

2.3定義配置類:

package config;

import bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import service.HelloService;

/**
 * @program: hello-spring-boot-starter
 * @description: 定義啟動(dòng)類starter的配置類;
 * @author: zdb
 * @motto: 認(rèn)真寫好每一行代碼
 * @create: 2021-09-11 16:58
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

?2.4在resources/META-INF定義spring.factories文件,指明開啟自動(dòng)配置類;

# Auto Configure 配置啟動(dòng)類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.HelloServiceAutoConfiguration

?3.測試

3.1創(chuàng)建一個(gè)springboot項(xiàng)目:hello-spring-boot-starter-test,創(chuàng)建一個(gè)HelloController類:

package com.zhangdb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import service.HelloService;

/**
 * @program: hello-spring-boot-starter
 * @description:
 * @author: zdb
 * @motto: 認(rèn)真寫好每一行代碼
 * @create: 2021-09-11 18:12
 */
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("hello/{name}")
    public String hello(@PathVariable(value = "name") String name){
        return helloService.sayHello(name);
    }
}

3.2引入自定義啟動(dòng)器starter:

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

        <!--引入自定義的start包-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.5.4</version>
        </dependency>
    </dependencies>

3.3在resources創(chuàng)建application.properties文件:可以配置自定義啟動(dòng)器的屬性:

zdb.hello.prefix = hi
zdb.hello.suffix = ABCDEF****

3.4完成了,可以測試;

原文鏈接:https://blog.csdn.net/zdb292034/article/details/120258715

欄目分類
最近更新