自己书写springboot-starter自动装配过程

springboot-starter自动装配

第一步新建springboot工程
删除resourses下的文件和启动类
在pom.xml中引入下面几个包

<!--引入spring-boot-starter:所有starter的基本配置
        这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- Compile dependencies 自动配置的包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <!--spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,
        就需要使用spring-boot-configuration-processor了-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

2.编写加载配置文件的类

/**
 * @author lzj
 * @date 2021/10/26
 * 加载配置文件的类 能够在springboot的配置文件中定义相应的属性被找到
 * 下面能找到lzj.user.username = lisi
 * 便可以更改值
 */
@ConfigurationProperties(prefix = "lzj.user")
public class UserProperties {
    private String username = "张三";
    private int age = 15;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

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

3.编写自动注入的类

@Component
public class UserServices {
    @Resource
    private UserProperties userProperties;
    public void say(){
        System.out.println(userProperties.getUsername()+" dsafas     age is" + userProperties.getAge());
    }
}

4.编写自动配置类

//声明配置类
@Configuration
//加载配置文件的类
@EnableConfigurationProperties({UserProperties.class})
//条件注解在有这个类的时候配置
@ConditionalOnClass(UserServices.class)
//条件注解 当lzj.user.enable=true时配置类生效
@ConditionalOnProperty(prefix = "lzj.user", value = "enable", matchIfMissing = true)
//需要继承InitializingBean接口为bean提供了初始化方法的方式
public class MystartAutoConfig implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
    }

    /**
     * 可以在里面配置你想自动装配的类
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public UserServices getUserServices() {
        return new UserServices();
    }
}

5.在resouses下新建META-INF/spring.factories
在这里插入图片描述
将我们写的自动配置类放到spring.factories文件中
springboot在初始化的时候会自动去扫这个文件,将文件中的类注入到容器中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jsu.lzj.spring.boot.autoconfigure.MystartAutoConfig

6.最后修改pom.xml打包安装到本地
build中留下面一个就够了
在这里插入图片描述
点击maven的install即可
在这里插入图片描述
打包好的jar包的使用时的目录
在这里插入图片描述

7.使用
在我们其它项目中就可以通过坐标引用了
在这里插入图片描述

8.更加规范的写法
像mybatis的starter一样
上面写的应该是直接自动配置的jar包

新建一个空的maven项目
只是用来管理jar包
只需要在pom.xml中引用我们的autoconfigure的jar包即可
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自定义Spring Boot Starter是一种用于简化Spring Boot应用程序配置和依赖管理的机制。它由几个组件组成,包括starter包、autoconfiguration包和配置文件。 首先,你需要创建一个Maven工程,并创建三个模块。其中,starter包负责引入依赖,autoconfiguration包负责实现装配。在autoconfiguration包中,你需要定义对应的properties类、configuration类和核心业务类。此外,你还需要在autoconfiguration包的/resources/META-INF/目录下添加spring.factories文件,并配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qiejk.demo.springboot.autoconfiguration.DemoAutoConfiguration。这一步是Spring Boot装配的核心,只有添加了这个内容,Spring Boot才会进行自动装配。\[1\] 在父模块的pom文件中,你需要引入Spring Boot的依赖,例如spring-boot-starter-web,以及指定Spring Boot的版本。这样,你的自定义starter模块就可以基于Spring Boot进行开发和使用。\[2\] 最后,你需要创建一个配置文件,用于告诉Spring Boot在启动时扫描该配置文件,并将其中的配置类加载到Spring容器中。这个配置文件的作用是指定自动配置类的位置。\[3\] 通过以上步骤,你就可以创建自定义的Spring Boot Starter,并在应用程序中使用它来简化配置和依赖管理。 #### 引用[.reference_title] - *1* [如何自定义springboot-starter](https://blog.csdn.net/sinat_29434159/article/details/123995794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot—自定义Starter篇](https://blog.csdn.net/m0_46571920/article/details/122910726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值