Stringboot实现自定义starter

本文详细介绍了如何创建一个自定义的Spring Boot Starter,包括删除不必要的依赖、配置服务、创建配置类和自动配置,以及在实际项目中使用这个Starter。通过创建autoconfigure项目和start项目,然后打包进Maven仓库,最后在新的Spring Boot项目中引入并测试。
摘要由CSDN通过智能技术生成

一、创建autoconfigure的SpringBoot项目

 

由spring-boot-starter可以发现,里面引入了一个spring-boot-autoconfigure的依赖,而这个依赖项目中都是已经写好的配置,所以我们自定义starter需要先创建一个autoconfigure项目来写配置

1.1、删除不需要的东西

将下面的依赖、启动类、测试类删除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1.2、配置

这里写一个service方法

public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String username){
        return helloProperties.getPrefix()+":"+username+">"+helloProperties.getSuffix();
    }
}

方法的返回值需要从配置文件中拿,所以建一个实体类与配置文件中的属性连接

//设置与配置文件中的前缀springboot.hello的属性进行连接
@ConfigurationProperties("springboot.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类并没有交给spring管理,所以需要建一个配置类将HelloService注入到spring容器中

@Configuration
@EnableConfigurationProperties(HelloProperties.class) //开启HelloProperties与配置文件的连接
public class HelloServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(HelloService.class) //当HelloService类不存在时才注入该类
    public HelloService helloService(){
        return new HelloService();
    }
}

1.3、将配置类的路径写入到META-INF目录下的spring.factories中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.springboot.hellospringbootautoconfigure.auto.HelloServiceAutoConfiguration

1.4、展示

完成的autoconfigure项目目录

 二、创建start的maven项目

只需要导入autoconfigure依赖即可

<?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.springboot</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.springboot</groupId>
            <artifactId>hello-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

三、打包进maven仓库

将这两个项目都打包进maven仓库中,以便引入的项目能找到

四、测试

新建springboot-web项目引入自定义starter依赖

<dependency>
    <groupId>com.springboot</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

 新建控制层,调用service方法

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("hello")
    public String sayService(){
        return helloService.sayHello("张三");
    }
}

 在配置文件中配置prefix和suffix属性的值

springboot:
  hello:
    prefix: "你好"
    suffix: 666

结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值