SpringBoot核心原理---自动配置 之创建自己的starter pom maven依赖包

上一篇:SpringBoot 的运行原理之自动配置,了解SpringBoot自动配置的原理,在此基础上动手做一个自己的自动配置依赖包。
本Demo源码:GitHub


一、准备工作

先创建Maven工程:
这里写图片描述
目录结构:
这里写图片描述


二、编码

MistraService.java
这个类就是要自动配置的Bean,条件就设为是否存在这个类的Bean,没有的话就创建。

package com.mistra.mistrastarter;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 是否进行自动配置的判断依据类,根据此类是否存在来决定是否创建这个类的Bean
 */
public class MistraService {

    private String name;

    public String sayYourName(){
        return "I'm " + name + "! ";
    }

    public String getName() {
        return name;
    }

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

MistraServiceProperties .java
为MistraService提供配置参数值得类。name设有默认值,配置文件配置前缀是"mistra"—>“mistra.name”,当没有在配置文件配置时就取默认值。

package com.mistra.mistrastarter;

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

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 自动配置的类型安全的注入参数类
 * 通过application.properties配置mistra.name的值来设置参数,若不设置,默认为"RoronoaZoro丶小王瑞"
 */
@ConfigurationProperties(prefix = "mistra")
public class MistraServiceProperties {

    private static final String NAME = "RoronoaZoro丶小王瑞";

    private String name = NAME;

    public String getName() {
        return name;
    }

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

MistraServiceAutoConfiguration.java
自动配置类。

package com.mistra.mistrastarter;

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

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 自动配置类
 * 根据条件判断是否要自动配置,创建Bean
 */
@Configuration
@EnableConfigurationProperties(MistraServiceProperties.class)
@ConditionalOnClass(MistraService.class)//判断MistraService这个类在类路径中是否存在
@ConditionalOnProperty(prefix = "mistra",value = "enabled",matchIfMissing = true)
public class MistraServiceAutoConfiguration {

    @Autowired
    private MistraServiceProperties mistraServiceProperties;

    @Bean(name = "mistraService")
    @ConditionalOnMissingBean(MistraService.class)//当容器中没有这个Bean时(MistraService)就自动配置这个Bean,Bean的参数来自于MistraServiceProperties
    public MistraService mistraService(){
        MistraService mistraService = new MistraService();
        mistraService.setName(mistraServiceProperties.getName());
        return mistraService;
    }
}

根据MistraServiceProperties提供的参数,判断MistraService这个类在类路径中是否存在,并且当容器中没有这个Bean(MistraService )时就自动配置创建这个Bean。
spring.factories
注册自动配置类。注意位置:src.main.resources.META-INF.spring.factories
这里写图片描述


三、maven打包

然后打包,并注册到本地maven仓库:
CMD 切换到项目根目录下:mvn install
这里写图片描述

然后在本地仓库就可以看见依赖包了:
这里写图片描述


四、引用

在别的项目引入依赖:

<dependency>
    <groupId>com.mistra</groupId>
    <artifactId>mistrastarter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

刷新Maven后可以在依赖里面找到自定义的包:
这里写图片描述
SpringBoot启动类TestApplication.java

@SpringBootApplication(scanBasePackages = "com.spring.boot.test")
public class TestApplication {

    public static void main(String[] args) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆   RoronoaZoro丶小王瑞   ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
        SpringApplication.run(TestApplication.class, args);
    }

在配置文件配置debug:true,可以看到自动配置的情况,可以找到自己写的配置类:
这里写图片描述

单元测试类StarterPomTest.java

package com.spring.boot.test.starter;

import com.mistra.mistrastarter.MistraService;
import com.spring.boot.test.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/26/026
 * Describe: 自定义starter测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class StarterPomTest {

    @Autowired
    private MistraService mistraService;

    @Test
    public void say() {
        System.out.println(mistraService.sayYourName());
    }
}

在配置文件中加入参数:

mistra:
  name: 罗罗诺亚索隆

如果不配置,就是取类中定义的默认值。
运行say():
这里写图片描述

至此,自定义的starter测试就完成了,也是对SpringBoot的核心自动配置的一次理解。
总结:
SpringBoot自动配置:@SpringBootApplication --> @EnableAutoConfiguration --> @Import({AutoConfigurationImportSelector.class}) -->AutoConfigurationImportSelector的selectImports()-getCandidateConfigurations() --> 调用SpringFactoriesLoader.loadFactoryNames()方法 --> 最后调用的loadSpringFactories()方法加载的“META-INF/spring.factories”文件

本Demo源码:GitHub


这里写图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Mybatis-Plus自动生成代码的步骤如下: 1. 首先,在pom.xml文件中添加Mybatis-Plus的依赖项。这括mybatis-plus-generator、velocity-engine-core和lombok等依赖项。\[1\] 2. 创建一个Mapper接口,继承自BaseMapper,并指定实体类的泛型。在这个接口中,你可以定义自己的查询方法。\[3\] 3. 配置Mybatis-Plus的代码生成器。你可以使用代码生成器来生成Mapper、Model、Service和Controller层的代码。你可以使用代码或者Maven插件来快速生成代码。\[2\] 4. 运行代码生成器,生成所需的代码文件。 5. 在Spring Boot的配置文件中配置数据库连接信息和Mybatis-Plus的相关配置。 6. 在Service层中使用生成的Mapper接口进行数据库操作。 通过以上步骤,你可以在Spring Boot中使用Mybatis-Plus自动生成代码。这样可以大大减少手动编写重复的CRUD操作的工作量,并提高开发效率。 #### 引用[.reference_title] - *1* [SpringBoot中的自动代码生成 - 基于Mybatis-Plus](https://blog.csdn.net/Jalon2015/article/details/116026730)[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整合mybatis-plus,代码自动生成](https://blog.csdn.net/qq_32784303/article/details/82964168)[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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值