Spring Boot之自定义Starter实现Demo

本篇博客我们来自己实现一个类似与其它Spring Boot的starter。

最简单的实现吧,不包括任何的业务逻辑。目的是只要引入了我们这个starter,就可以自动配置我们这个项目中的一个Bean(我们的例子中的FooService)。

首先肯定是要创建一个Project了,我们直接创建一个Maven项目就可以了。

首先,需要在POM中添加Spring Boot AutoConfigure的依赖。

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

然后,再定义一个FooProperties的类,来承载Spring Boot项目中application.properties中定义的各种k-v。

    package org.example;
     
    import org.springframework.boot.context.properties.ConfigurationProperties;
     
    @ConfigurationProperties(prefix = "hello")
    public class FooProperties{
     
        private final static String MSG = "world";
     
        private String msg = MSG;
     
        public static String getMSG() {
            return MSG;
        }
     
        public String getMsg() {
            return msg;
        }
     
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

接下来我们定义一个Service,命名为FooService。

    package org.example;
     
    /**
     * @author Tyler
     */
    public class FooService {
     
        private String msg;
     
        public String sayHello() {
            return "Hello " + msg;
        }
     
        public String getMsg() {
            return msg;
        }
     
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

 Service中只有一个sayHello方法,返回“Hello”+msg属性。

然后呢我们需要再定义一个AutoConfiguration的类,这里就叫FooAutoConfiguration吧。SpringBoot在启动容器时,会扫描所有引入的starter的Jar包里面的META-INF目录下的spring.factories文件,获取到需要加载的AutoConfiguration类。因此,我们这个Demo中,这个AutoConfiguration才是最最最重要的,SpringBoot会自动加载,然后我们要实现的是,通过这个类来实现我们的业务逻辑(我们这里就是需要配置FooService的Bean)。

    package org.example;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
     
    /**
     * @author Tyler
     */
    @Configuration
    @ConditionalOnClass(FooService.class)
    @EnableConfigurationProperties(FooProperties.class)
    @ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
    public class FooAutoConfiguration {
     
        @Autowired
        private FooProperties fooProperties;
     
        @Bean
        @ConditionalOnMissingBean(FooService.class)
        public FooService helloService() {
            FooService service = new FooService();
            service.setMsg(fooProperties.getMsg());
            return service;
        }
    }

最后我们需要在resources目录下新建一个META-INF的目录,然后在其下创建一个spring.fatories的文件:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.FooAutoConfiguration

到这里我们就已经大功告成了。

下面让我们试试我们的Demo能不能Work。

1、在上边的starter项目中mvn install 安装到本地仓库。

2、新建一个SpringBoot项目,添加上边demo-starter和spring-boot-starter-web的依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>spring-starter-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

3、编写Controller,依赖FooService

    package com.example.demo.controller;
     
    import org.example.FooService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class DemoController {
     
        @Autowired
        private FooService fooService;
     
        @GetMapping("/")
        public String index() {
            return fooService.sayHello();
        }
    }

4、在项目的application.properties文件中定义FooProperties的msg属性

hello.msg=tyler

5、启动项目,浏览器访问

Perfect!O(∩_∩)O哈哈~

转自:https://blog.csdn.net/qq_33561055/article/details/105467873

参考:https://www.cnblogs.com/laoxia/p/13469915.html

posted @ 2021-06-04 17:12  甜菜波波  阅读(189)  评论(0编辑  收藏  举报