首页 > 编程语言 > 详细

SpringBoot自定义starters

时间:2020-09-13 10:49:28      阅读:68      评论:0      收藏:0      [点我收藏+]

1 自定义starter的原理

  • 1??这个场景需要使用的依赖是什么?
  • 2??如果编写自动配置。
@Configuration  //指定这个类是配置
@ConditionalOnXxx //在指定条件成立的情况下自动配置类生成
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationProperties结合相关的XxxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让XxxProperties类生效,并加入到容器中    
    
自动配置类要能加载,需要将其配置到META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,...   
  • 3??启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。

技术分享图片

2 自定义starter

2.1 使用IDEA新建一个空的工程spring-boot-starter

技术分享图片

2.2 新建sunxiaping-spring-boot-starter-autoconfigurer工程

  • 导入相关jar的Maven坐标:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sunxiaping.starter</groupId>
    <artifactId>sunxiaping-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1</version>
    <name>sunxiaping-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

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


</project>
  • 新建HelloProperties.java
package com.sunxiaping.starter;

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

@ConfigurationProperties(prefix = "sunxiaping.hello")
public class HelloProperties {

    private String prefix;

    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
  • 新建HelloService.java
package com.sunxiaping.starter;

public class HelloService {

    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name) {
        return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
    }

}
  • 新建HelloServiceAutoConfiguration.java
package com.sunxiaping.starter;

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

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

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

}
  • 在resource目录下新建META-INF目录,并在其新建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sunxiaping.starter.HelloServiceAutoConfiguration

2.3 新建sunxiaping-spring-boot-starter工程

  • 新建相关jar包的Maven坐标:
<?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.sunxiaping.starter</groupId>
    <artifactId>sunxiaping-spring-boot-starter</artifactId>
    <version>1.0</version>

    <!--  启动器  -->
    <dependencies>
        <!--   引入自动配置模块     -->
        <dependency>
            <groupId>com.sunxiaping.starter</groupId>
            <artifactId>sunxiaping-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

</project>

SpringBoot自定义starters

原文:https://www.cnblogs.com/xuweiweiwoaini/p/13660381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!