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

    }
}

相关推荐
逆境不可逃1 小时前
Java JUC 同步工具类一次讲透:CountDownLatch、CyclicBarrier、Semaphore、Phaser 与 AQS 共享模式
java·开发语言·python
xlq223222 小时前
高并发服务器day5
java·服务器·数据库
our_times2 小时前
【硬核实战】当机器人走进厨房:Java后端如何重构物联网并发与状态一致性`。
java·重构·机器人
canonical_entropy2 小时前
Mission Driver:Loop Engineering 的一种通用参考实现
后端·aigc·ai编程
一路向北North2 小时前
Spring AI(5) :对话机器人-会话记忆
java·后端·spring
CappuccinoRose2 小时前
Rust学习文档(二)
开发语言·后端·学习·rust
kingwebo'sZone2 小时前
C#事件声明办法
java·前端·c#
Q渡劫2 小时前
MinIO 从下载到集成 Spring Boot 完整教程
spring boot·spring·maven
冻感糕人~2 小时前
大模型学习指南:收藏这份AI Agent四层工程地图(小白程序员必备)
java·大数据·人工智能·学习·大模型·agent·大模型学习