SpringBoot使用自动装配编写Start包

参考资料:

参考demo


具体过程:

  1. 项目结构

hello-spring-boot-starter

│

├── pom.xml

│

├── src

│ └── main

│ ├── java

│ │ └── com

│ │ └── example

│ │ └── hellostarterdemo

│ │ ├── HelloProperties.java

│ │ ├── HelloService.java

│ │ └── HelloAutoConfiguration.java

│ │

│ └── resources

│ └── META-INF

│ └── spring.factories

  1. pom.xml
XML 复制代码
<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
         https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- Starter核心依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <!-- 自动生成配置提示(可选) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>
  1. HelloProperties
java 复制代码
package com.example.hellostarterdemo;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    /**
     * 默认值
     */
    private String name = "World";

    public String getName() {
        return name;
    }

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

}
  1. HelloService
java 复制代码
package com.example.hellostarterdemo;

public class HelloService {

    private final HelloProperties properties;

    public HelloService(HelloProperties properties) {
        this.properties = properties;
    }

    public String sayHello() {

        return "Hello " + properties.getName();

    }

}
  1. HelloAutoConfiguration
java 复制代码
package com.example.hellostarterdemo;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService(HelloProperties properties) {

        return new HelloService(properties);

    }

}
  1. spring.factories
复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.hellostarterdemo.HelloAutoConfiguration
  1. 然后执行命令
bash 复制代码
mvn clean install

使用过程:

  1. 添加依赖
XML 复制代码
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>
  1. application.yml
python 复制代码
hello:
  name: GPT
  1. Controller
java 复制代码
package com.example.demo.controller;

import com.example.hellostarterdemo.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("starter")
public class StarterController {
    @Autowired
    private HelloService helloService;

    @GetMapping("hello")
    public String hello() {

        return helloService.sayHello();

    }
}

相关推荐
Zhou14113621 分钟前
SpringBoot_02_自动配置原理
java·spring boot·后端
FYKJ_201027 分钟前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
kv11031 分钟前
Android studio国内开发有关
java·数据库·android studio
卓怡学长1 小时前
w198基于springboot始于足下健康打卡平台的设计与实现
java·spring boot·spring·intellij-idea
阿狗童鞋1 小时前
Java并发编程实战指南
java·开发语言
子兮曰1 小时前
Node.js 50个优势场景盘点:一个人单干,为啥我多数时候只用它
前端·后端
IT_陈寒1 小时前
我花3小时debug,就因为JavaScript这个隐式转换坑
前端·人工智能·后端
Joe_Wang52 小时前
【从0到1学习JVM · 15】对象在堆里来回倒腾,究竟怎样才会进老年代?
java·jvm·学习·垃圾回收
程序猿阿越2 小时前
containerd如何拉取镜像
后端·kubernetes·源码阅读
一条小小yu2 小时前
为什么使用springboot
java·spring boot·后端