SpringBoot中如何详细实现自定义Starter步骤解析?

2026-05-25 20:261阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计662个文字,预计阅读时间需要3分钟。

SpringBoot中如何详细实现自定义Starter步骤解析?

目录:+ Starter 起步依赖 + Starter 命名规范 + 自定义 Starter + new module + 添加依赖 + SimpleBean + 自动配置类 + META-INF/spring.factories + 在 spring-boot-mytest 中引入 mystarter-spring-boot-starter + 添加配置 + 通过 @Autowired 引用 + 启动

目录
  • starter起步依赖
  • starter命名规则
  • 自定义starter
    • new module
    • 添加依赖
    • simplebean
    • 自动配置类
    • META-INF\spring.factories
    • 在spring-boot-mytest中引入mystarter-spring-boot-starter
    • 添加配置
    • 通过@Autowired引用
    • 启动访问

starter起步依赖

starter起步依赖是springboot一种非常重要的机制,

它打包了某些场景下需要用到依赖,将其统一集成到starter,

比如,

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

这就是一个starter,你可以把它看做一个外部外部项目,注意:是外部项目。

starter命名规则

springboot提供的starter以spring-boot-starter-x的方式命名,

自定义starter以x-spring-boot-starter的方式命名,

以区分springboot生态提供的starter。

自定义starter

new module

mystarter-spring-boot-starter

SpringBoot中如何详细实现自定义Starter步骤解析?

maven项目

添加依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.2.9.RELEASE</version> </dependency>

load maven changes

simplebean

package com.duohoob.bean; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 这两个都是非@Component注解, * 否则springboot启动时会直接被加载进spring容器 */ @EnableAutoConfiguration @ConfigurationProperties(prefix = "simplebean") public class SimpleBean { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "SimpleBean{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } }

自动配置类

package com.duohoob.config; import com.duohoob.bean.SimpleBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyAutoConfiguration { @Bean public SimpleBean simpleBean() { return new SimpleBean(); } }

META-INF\spring.factories

在resources下创建META-INF\spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.duohoob.config.MyAutoConfiguration

在spring-boot-mytest中引入mystarter-spring-boot-starter

<dependency> <groupId>com.duohoob</groupId> <artifactId>mystarter-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>

load maven changes

添加配置

在spring-boot-mytest的src/main/resources/application.properties中添加配置

通过@Autowired引用

package com.duohoob.springbootmytest.controller; import com.duohoob.bean.SimpleBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private SimpleBean simplebean; @RequestMapping("/test") public String test() { return simplebean.toString(); } }

启动访问

到此这篇关于SpringBoot自定义Starter实现流程详解的文章就介绍到这了,更多相关SpringBoot自定义Starter内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计662个文字,预计阅读时间需要3分钟。

SpringBoot中如何详细实现自定义Starter步骤解析?

目录:+ Starter 起步依赖 + Starter 命名规范 + 自定义 Starter + new module + 添加依赖 + SimpleBean + 自动配置类 + META-INF/spring.factories + 在 spring-boot-mytest 中引入 mystarter-spring-boot-starter + 添加配置 + 通过 @Autowired 引用 + 启动

目录
  • starter起步依赖
  • starter命名规则
  • 自定义starter
    • new module
    • 添加依赖
    • simplebean
    • 自动配置类
    • META-INF\spring.factories
    • 在spring-boot-mytest中引入mystarter-spring-boot-starter
    • 添加配置
    • 通过@Autowired引用
    • 启动访问

starter起步依赖

starter起步依赖是springboot一种非常重要的机制,

它打包了某些场景下需要用到依赖,将其统一集成到starter,

比如,

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

这就是一个starter,你可以把它看做一个外部外部项目,注意:是外部项目。

starter命名规则

springboot提供的starter以spring-boot-starter-x的方式命名,

自定义starter以x-spring-boot-starter的方式命名,

以区分springboot生态提供的starter。

自定义starter

new module

mystarter-spring-boot-starter

SpringBoot中如何详细实现自定义Starter步骤解析?

maven项目

添加依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.2.9.RELEASE</version> </dependency>

load maven changes

simplebean

package com.duohoob.bean; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 这两个都是非@Component注解, * 否则springboot启动时会直接被加载进spring容器 */ @EnableAutoConfiguration @ConfigurationProperties(prefix = "simplebean") public class SimpleBean { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "SimpleBean{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } }

自动配置类

package com.duohoob.config; import com.duohoob.bean.SimpleBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyAutoConfiguration { @Bean public SimpleBean simpleBean() { return new SimpleBean(); } }

META-INF\spring.factories

在resources下创建META-INF\spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.duohoob.config.MyAutoConfiguration

在spring-boot-mytest中引入mystarter-spring-boot-starter

<dependency> <groupId>com.duohoob</groupId> <artifactId>mystarter-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>

load maven changes

添加配置

在spring-boot-mytest的src/main/resources/application.properties中添加配置

通过@Autowired引用

package com.duohoob.springbootmytest.controller; import com.duohoob.bean.SimpleBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private SimpleBean simplebean; @RequestMapping("/test") public String test() { return simplebean.toString(); } }

启动访问

到此这篇关于SpringBoot自定义Starter实现流程详解的文章就介绍到这了,更多相关SpringBoot自定义Starter内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!