超简单 定义一个自己的starter

自定义starter的作用

开发的时候,经常有一些独立于业务之外的配置模块。如果将这些可独立与业务的模块,配置成一个个的starter,复用的时候就更加方便了。只需要pom中引入,项目中就可以由springboot帮我们完成自动装配。关于自动装配,大家可以参考这篇博客 从注解入手扒一扒SpringBoot的自动配置原理



一、自定义starter

1、starter的命名

springboot官方提供的starter一般以spring-boot-starter-xxxx方式命名,官方建议自定义的starter使用xxxx-spring-boot-starter命名。用来区分第三方的starter和springboot官方的starter。

2、创建maven项目 引入springboot的autoconfigure

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

3、需要自动配置的业务Bean

这里简单配置了一个javaBean,功能是读取配置文件中的属性,映射成这个Bean。但是仅仅这么配置,Spring扫描时,并不会注册这个Bean。因为Spring的启动过程中的自动配置,是查找classpath上所有jar包中的META-INF/spring.factories 和 .imports文件中的配置类,进行的自动装配。仅仅定义一个Bean是不会被扫描的。

	@EnableConfigurationProperties(SimpleBean.class)
    @ConfigurationProperties(prefix = "simplebean")
    public class SimpleBean {
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int 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 + '\'' +
                '}';
        }
    }

4、创建配置类,向容器类中注入第三步需要生成的Bean

//@ConditionalOnClass:当类路径classpath下有指定的类的情况下进行自动配置,此处表示无限制,直接自动注册
@Configuration
@ConditionalOnClass
public class MyAutoConfiguration {
  static {
    System.out.println("MyAutoConfiguration init....");
 }
    
  @Bean
  public SimpleBean simpleBean(){
    return new SimpleBean();
 }
    
}

5、编写/META-INF/spring.factories供SpringBoot扫描


1.在resource下创建文件 /META-INF/spring.factories
在这里插入图片描述

2.配置spring.factories中的EnableAutoConfiguration属性 。

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



二、测试自定义starter


1、springboot项目中引入刚刚创建的starter

<dependency>
  <groupId>priv.lyp</groupId>
  <artifactId>zdy-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

2、springboot项目配置文件中,定义starter需要的配置文件

simplebean.id=1
simplebean.name=自定义starter

3、直接尝试注入自动配置的Bean

//测试自定义starter
@Autowired
private SimpleBean simpleBean;
@Test
public void zdyStarterTest(){
	System.out.println(simpleBean);
}



总结

mybatis-plus提供的starter
从mybatis提供的自定义的starter结构可以看出,starter就是一个普通的maven项目,引入了需要配置的模块后,帮用户写好接入Spring需要的配置操作,再进行一次封装。用户就可以省去配置的过程,做到引入就自动配置。这一点也符合Spring在官网上,对SpringBoot的介绍_ Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示了如何手写一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值