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();

    }
}

相关推荐
鲸采云SRM采购管理系统几秒前
采购管理系统哪家好?2026最新测评对比
java·服务器·数据库
IT_陈寒13 分钟前
Python的GIL让我多写了500行代码
前端·人工智能·后端
0x531 小时前
Java网络编程(二)
java·服务器·网络
Flittly1 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
Poo_Chai1 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
IT爱学堂1 小时前
精讲软件设计师(软考中级)一站式通关课_实战课程_慕课网
后端
瑞码空间1 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
树码小子1 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
战天战地站房顶2 小时前
在 Windows Docker 中部署 PostgreSQL 17 + pgvector 完整指南
后端
卷无止境2 小时前
FastAPI Guard 全解析,从概念到工程落地的实战指南
后端·python