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

    }
}

相关推荐
努力的小郑8 分钟前
数据查到了,回答去哪儿了?一次百炼 + LangChain 排查实录
后端·langchain·agent
SL_staff34 分钟前
JVS-BI 实战:业务人员如何用零代码 ELT + 可视化配置构建实时经营分析闭环
java·spring·全栈
isfox1 小时前
Python 虚拟环境:别再把所有项目塞进一个"大仓库"了
后端
SL_staff1 小时前
企业AI落地难?JVS-AI渐进式实践:安全可控、小切口验证、标准化沉淀与规模化复制
java·开源·aigc
易师傅1 小时前
技术面试已死?AI 时代,真正拉开程序员差距的是什么?
前端·后端·程序员
苍何1 小时前
办公 Agent 权威评测榜单出炉了,豆包第一
后端
AiMan2 小时前
别再把秘塔会话逐个点开:这可把秘塔多个会话一次导出?AI导出鸭
前端·后端·面试
SL_staff2 小时前
JVS数字底座实践:构建企业级统一内容管理平台的技术路径
java·产品
nice_lcj5202 小时前
【从零理解 Spring AI · 12】MCP 的两种角色:做客户端还是做服务端
java·后端·spring·spring ai·ai应用开发
卷无止境3 小时前
mathmodel-figure-templates 以及两个上手案例
后端·python