springBoot自定義starter

新建一個(gè)mave項(xiàng)目

1、導(dǎo)入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.start</groupId>
    <artifactId>hello-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

    </dependencies>
</project>

2、定義一個(gè)配置類

@ConfigurationProperties("hello")
public class HelloProperties {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3、定義一個(gè)service

public class HelloService {
    private String name;
    private int age;

    public HelloService(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void hello() {
        System.out.println(name + "  age:" + age);
    }
}

4、配置自動(dòng)裝載

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        return new HelloService(helloProperties.getName(), helloProperties.getAge());
    }
}

5、配置spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.start.HelloServiceAutoConfiguration
image.png

自定義starter已完成,下面開始測(cè)試

重新建一個(gè)maven工程

1、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.start</groupId>
  <artifactId>start-demo-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
  </parent>
  <name>start-demo-test Maven Webapp</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.start</groupId>
      <artifactId>hello-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

2、啟動(dòng)類

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author: zhouwei
 * @version: v1.0
 * @description: PACKAGE_NAME
 * @date:2021-01-20
 */
@SpringBootApplication public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

3、配置文件

image.png

4、測(cè)試controller

package com.test;

import com.start.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: zhouwei
 * @version: v1.0
 * @description: PACKAGE_NAME
 * @date:2021-01-20
 */
@RestController
@RequestMapping("/api")
public class TestController {
    @Resource(name = "helloService") private HelloService helloService;

    @GetMapping("test") public String get() {
        helloService.hello();
        return "success";
    }
}

啟動(dòng)打印結(jié)果:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 為什么需要自制啟動(dòng)器?因?yàn)闃O大的提高了代碼的高可用,比如在我們的日常開發(fā)工作中,如果一個(gè)工程需要復(fù)用一些獨(dú)立于業(yè)務(wù)...
    CodePandaes閱讀 392評(píng)論 0 2
  • 1.創(chuàng)建自己的Starter 一個(gè)完整的Spring Boot Starter可能包含以下組件: autoconf...
    java菜閱讀 2,142評(píng)論 0 0
  • SpringBoot自定義starter 源碼地址:https://github.com/YuSheng1223/...
    java晉升閱讀 1,208評(píng)論 0 1
  • SpringBoot自定義Starter 再面試的時(shí)候被問到會(huì)不會(huì)自定義Starter 尷尬了。。所以回來百度了下...
    神豪VS勇士贏閱讀 1,247評(píng)論 0 3
  • 自定義starter starter:這個(gè)場(chǎng)景需要使用到的依賴是什么?如何編寫自動(dòng)配置@Configuration...
    eliteTyc閱讀 512評(píng)論 0 0

友情鏈接更多精彩內(nèi)容