SpringBoot自定义starter入门

本文介绍了如何创建一个SpringBootStarter,包括修改pom.xml配置,定义实体类接收application.properties的值,创建服务类,编写自动配置类,配置spring.factories文件,并说明了如何在本地安装和在其他项目中引用及测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建一个普通的Maven项目

 pom.xml文件修改

<?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>org.example</groupId>
    <artifactId>Starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--添加Starter自动化配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.7.11</version>
        </dependency>
    </dependencies>
</project>

二、创建一个实体类,接受application.properties中注入的值

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

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 自定义类,接受application.properties中注入的值
 */
@ConfigurationProperties(prefix = "my")
public class MyProperties {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

三、自定义服务类

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 自定义服务类
 */
public class MyService {

    private String name;

    private Integer age;


    public String introduce() {
        return "我的姓名" + name + ",今年" + age + "岁";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

四、创建一个自动配置类

import com.example.starter.properties.MyProperties;
import com.example.starter.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 自动配置类
 */
@Configuration
@EnableConfigurationProperties(MyProperties.class)
@ConditionalOnClass(MyService.class)
public class MyServiceAutoConfiguration {

    @Autowired
    private MyProperties myProperties;

    /**
     * 注入MyService到Spring容器
     */
    @Bean
    public MyService myService() {
        MyService myService = new MyService();
        myService.setName(myProperties.getName());
        myService.setAge(myProperties.getAge());
        return myService;
    }
}

@Configuration注解表示这是一个配置类。

@EnableConfigurationProperties注解可以使我们之前在MyProperties类中配置的@ConfigurationProperties生效,让配置的属性加载到Spring容器中。

@ConditionalOnClass注解表示当前存在MyService时,后面的配置才会生效。

自动配置类中首先注入MyProperties,提供一个MyService对象,并把这个对象加载到Spring的容器中。

五、spring.factories文件的配置

我们需要在spring.factories文件中定义需要加载的自动化配置类,我们需要在Maven项目中的resources目录下创建一个名为META-INF的文件夹,然后在这个文件夹里面创建一个名为spring.factories的文件,文件内容指定自定义配置类的路径:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.starter.config.MyServiceAutoConfiguration

六、本地安装

我们把刚才创建的Maven项目上传到本地的Maven仓库中,然后就可以在其他项目中使用了。

 我们使用Maven下的install命令,把Maven上传到我们的本地的Maven仓库。

七、在其他项目使用

1.添加依赖 

<!--引入自定义starter-->
<dependency>
    <groupId>org.example</groupId>
    <artifactId>Starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2.在application.yml进行自定义的属性配置

my:
  name: lisi
  age: 28

 3.在单元测试中进行测试

import com.example.starter.service.MyService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemoApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    void contextLoads() {
        System.out.println(myService.introduce());
    }

}

 完成了自定义的Starter的创建和使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qinxun2008081

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

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

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

打赏作者

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

抵扣说明:

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

余额充值