Spring boot自定义starter

Spring boot自定义starter
1、简介
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。
2、如何自定义starter
2.1、官方命名空间
• 前缀:spring-boot-starter-
• 模式:spring-boot-starter-模块名
• 举例:spring-boot-starter-web、spring-boot-starter-jdbc
2.2、自定义命名空间
• 后缀:-spring-boot-starter
• 模式:模块-spring-boot-starter
• 举例:demo-spring-boot-starter
2.3、定义starter需要的配置
根据springboot的自动配置原理,可知自定义starter需要
• 一个xxxxAutoConfigurartion的配置模块
• 一个xxxxProperties:封装配置文件中相关属性
• 需要一个META-INF/spring.factories文件来描述配置文件工厂类
3、自定义starter实例
3.1、创建一个maven工程
项目名:demo-spring-boot-starter
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.start</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- 引入自动配置模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>
    <!-- 打包插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2、创建DemoProperties属性类

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.start")
public class DemoProperties {
    private String name = "demo";
    private int age = 20;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,便使用默认的,如上name=“demo”,age=20.
3.3、创建要注入的组件类DemoUtil

public class DemoUtil {
    private DemoProperties demoProperties;
    public DemoProperties getDemoProperties() {
        return demoProperties;
    }
    public void setDemoProperties(DemoProperties demoProperties) {
        this.demoProperties = demoProperties;
    }
    public String getSomeInfo(){
        return demoProperties.getName()+":"+demoProperties.getAge();
    }
}

自定义starter的核心业务,满足业务需求的部分。
3.4、创建自动配置类

@Configuration
@EnableConfigurationProperties({DemoProperties.class})
@ConditionalOnClass(DemoUtil.class)
@ConditionalOnWebApplication
@ConditionalOnProperty(
        prefix = "com.start",
        value = {"enabled"},
        matchIfMissing = true
)
public class DemoAutoConfiguration {
    private final DemoProperties demoProperties;
    public DemoAutoConfiguration(DemoProperties demoProperties) {
        this.demoProperties = demoProperties;
    }
    @Bean
    @ConditionalOnMissingBean(DemoUtil.class)
    public DemoUtil demoUtil(){
        DemoUtil demoUtil = new DemoUtil();
        demoUtil.setDemoProperties(demoProperties);
        return demoUtil;
    }
}

• @Configuration:表明此类是一个配置类,将做为一个bean被spring进行管理。
• @EnableConfigurationProperties:启用属性配置,将读取DemoProperties里面的属性。
• @ConditionalOnClass:当类路径下面有DemoUtil此类时,自动配置。
• @ConditionalOnProperty:判断指定的属性是否具备指定的值。
• @ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。
• @Bean给容器中添加一个组件
3.5、创建spring.factories文件
在resources文件夹下创建META-INF文件夹,创建spring.factories文件,在spring.factories编写DemoAutoConfiguration配置工厂类描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.start.DemoAutoConfiguration

3.6、对项目进行打包
对项目进行mvn clean install操作会得到一个demo-spring-boot-starter-1.0-SNAPSHOT.jar,在其他项目可以在pom中引入

<dependency>
    <groupId>com.start</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3.7、测试
创建一个测试项目引入demo-spring-boot-starter-1.0-SNAPSHOT.jar,编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootLearnApplicationTests {
    //记录器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DemoUtil demoUtil;
    @Test
    public void testSpringStart(){
        System.out.println(demoUtil.getSomeInfo());
    }
}

观察控制台打印的日志

demo:18
2021-09-24 17:06:54.540  INFO 18060 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@1a20270e: startup date [Fri Sep 24 17:06:52 CST 2021]; root of context hierarchy
Disconnected from the target VM, address: '127.0.0.1:51896', transport: 'socket'

可以看到打印信息ok
在配置文件中添加自定义配置

com.start.name=tom
com.start.age=20

再次启动测试类,观察打印日志

tom:20
2021-09-24 17:08:11.707  INFO 22704 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@1a20270e: startup date [Fri Sep 24 17:08:10 CST 2021]; root of context hierarchy
Disconnected from the target VM, address: '127.0.0.1:51941', transport: 'socket'

可以看到配置生效了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_46007090

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值