springboot系列之自定义starter实例

本文介绍了使用Idea搭建Spring Boot启动器项目的步骤。先建立empty project,添加自动配置和启动器两个module,启动器依赖自动配置。接着说明了项目结构和内部代码,最后进行测试,新建Spring Boot项目引入启动器,添加配置项,通过浏览器或Postman请求获得响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、idea建立一个empty project。

2、添加两个module,一个是自动配置(maven工程),一个是启动器(springboot工程),启动器依赖自动配置。

3、项目结构

4、内部代码

1)spring-boot-starter-autoconfigurer module:

package com.wms.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAuto {
    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
package com.wms.starter;

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

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
@ConfigurationProperties(prefix = "wms.hello")
public class HelloProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.wms.starter;

/**
 * @author:wangmingsai
 * @date:2018/8/28
 * @desc:
 */
public class HelloService {

    HelloProperties helloProperties;

    public String hello() {
        return "hello" + helloProperties.getName();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}
spring.factories文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wms.starter.HelloAuto
pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.wms</groupId>
	<artifactId>spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

	</dependencies>

</project>

2)springbootstarter:

启动器比较简单,只有pom文件,引入自动配置即可。

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>wms</groupId>
    <artifactId>springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.wms</groupId>
            <artifactId>spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

 5、测试,新建springboot项目,pom文件中引入启动器

        <dependency>
            <groupId>wms</groupId>
            <artifactId>springboot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

application.properties添加配置项 

wms.hello.name=chenzz

 测试controller:

package com.wms.teststarter;

import com.wms.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:wangmingsai
 * @date:2018/8/29
 * @desc:
 */
@RestController
public class controller {

    @Autowired
    HelloService helloService;

    @GetMapping("hello")
    public String hello(){
        return helloService.hello();
    }
}

通过浏览器或者postman请求,获得响应:

### 如何创建和使用 Spring Boot 自定义 Starter #### 创建自定义 Starter 项目结构 为了构建一个可重用的组件,通常会创建一个新的 Maven 或 Gradle 项目来作为自定义 Starter 的容器。这个项目的 `pom.xml` 文件应该包含必要的依赖项以及打包方式设置为 jar。 对于名为 `test-my-starter` 的 Spring Boot 项目,在其 `pom.xml` 中引入特定于业务逻辑或其他功能模块所需的第三方库或框架支持[^3]: ```xml <dependencies> <!-- 引入其他所需依赖 --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` #### 编写自动配置类 在自定义 Starter 内部实现自动化配置的关键在于编写带有条件化注解(如 @ConditionalOnClass, @ConditionalOnMissingBean 等)的 Java 类,并将其注册给应用程序上下文以便被发现与应用。这些条件允许开发者控制何时激活某些 Bean 定义或者行为模式[^2]。 假设要开发一个简单的 Hello Service,则可以按照下面的方式设计服务接口及其默认实现形式: ```java package com.example.testmystarter; public interface MyService { String sayHello(); } @Component @ConditionalOnProperty(name = "example.enabled", havingValue = "true", matchIfMissing = true) public class DefaultMyServiceImpl implements MyService { private final Environment environment; public DefaultMyServiceImpl(Environment environment) { this.environment = environment; } @Override public String sayHello() { return "Hello from Test My Starter!"; } } ``` #### 注册自动配置器 为了让 Spring Boot 在启动过程中识别并加载上述编写的自动配置类,还需要通过资源目录下的 `META-INF/spring.factories` 文件声明该类的位置。此文件的内容应当遵循键值对的形式,其中键固定为 org.springframework.boot.autoconfigure.EnableAutoConfiguration[]^4]^: ``` # META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.testmystarter.MyServiceAutoConfiguration ``` 这里假定存在这样一个名为 `MyServiceAutoConfiguration.java` 的类用于管理所有关于 `MyService` 接口实例化的规则。 #### 发布至本地/远程仓库 当完成了以上步骤之后就可以考虑将所制作好的 Starter 打包发布出去供他人引用了。如果是在团队内部共享的话可以直接推送到私有 Nexus/Sonatype Artifactory 上面;而对于开源社区而言则可以选择提交到中央仓库或者其他公共平台上去[^5]。 最后一步就是确保任何想要利用我们新建立起来的功能扩展的人都可以在自己的工程里轻松地添加如下所示的一行 POM 配置即可完成集成工作: ```xml <!-- 将自定义 starter 添加到目标项目的 pom.xml 中 --> <dependency> <groupId>com.example</groupId> <artifactId>test-my-starter</artifactId> <version>${latest.version}</version> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值