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

    }
}

相关推荐
邪修king6 小时前
Re:Linux 系统篇(三十五):进程间通信Chapter2:从匿名管道到进程池:Linux 进程间通信实战(深度理解「先描述,再组织」)
java·linux·运维
拆房老料6 小时前
【第四部分:Agentic 智能体设计模式】16. 一个 Prompt 搞不定复杂任务?试试 Prompt Chaining
java·前端·人工智能·设计模式·prompt
张张123y7 小时前
后端第十四课:Agent 记忆系统(让 Agent 记住上下文和用户,跨会话不健忘)
java·大数据·人工智能·python·agent·记忆
考虑考虑11 小时前
nginx路由重写
运维·后端·自动化运维
曹牧11 小时前
C#:空合并运算符
java·前端·数据库
布吉岛的石头12 小时前
Java 程序员第 50 阶段2:预训练的目标函数:自回归语言建模本质
java·开发语言·人工智能·深度学习·回归·transformer
golang学习记12 小时前
IDEA 2026年的玩法已经完全不同
java·ide·intellij-idea
JaguarJack12 小时前
PHP 紧急修复两项严重 RCE 漏洞,8.2 至 8.5 用户应立即升级
后端·php·服务端
BingoGo12 小时前
PHP 紧急修复两项严重 RCE 漏洞,8.2 至 8.5 用户应立即升级
后端·php
紧跟先前的步伐12 小时前
Go AI Agent 实战 05|Temperature 与生成参数:让模型输出可控
java·人工智能·golang