SpringBoot实践(三十八):自定义spring-boot-starter

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

自动配置原理

 自定义starter包

导入springboot的自动配置依赖 

测试业务代码

spring.factories配置

​编辑

 本地包上传

使用自定义starter依赖

测试和配置


自动配置原理

基于springBoot的starter机制能够让我们在使用外部包时候非常方便只需要引入该组件提供的starter包即可比如在pom中引入mybatis的mybatis-plus-boot-starter依赖包那么springboot启动时候会自动扫描所有的依赖该包下META-INF文件夹下的spring.factories把自动配置类加入容器

在spring.factories中会指定自动配置类

# Auto Configure
org.springframework.boot.env.EnvironmentPostProcessor=\
  com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

其中MybatisPlusAutoConfiguration.java中会初始化若干的配置在MybatisPlusProperties.class文件中还有其他一些关于jdbc连接的条件注解

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({MybatisPlusProperties.class})

 自定义starter包

测试自定义starter包该starter包中引入springboot的自动配置依赖配置spring.factories文件中的自动加载类实现的功能是在测试的springBoot工程中引入自定义的starter包的pom依赖后能够调用starter包中的某些实例方法具体而言是读取application.properties或者application.yml的配置输出

首先创建1个空的项目作为自定义starter包和测试的springboot工程的父工程添加2个子模块

导入springboot的自动配置依赖 

创建spring初始化器生成的子模块test01-spring-boot-starter跟mybatis类似或者直接copy增加spring-boot-starter的依赖管理不引入只引入1个自动配置依赖

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.3.7.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<scope>compile</scope>
		</dependency>
	</dependencies>

测试业务代码

创建1个属性配置类TestProperties.java读取application.properties中的配置

package com.example.hello.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "test.hello")
public class TestProperties {
    String prefix;
    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;
    }
}

 创建业务代码实现模拟的业务需求它使用上面的配置类读取配置做业务处理直接输出

package com.example.hello.service;
import com.example.hello.bean.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;


public class HelloService {

    @Autowired
    TestProperties testProperties;

    public String sayHello() {
        return testProperties.getPrefix() + "<---- say hello --->" + testProperties.getSuffix();
    }

}

再创建1个自动配置类TestAutoConfiguration.java该类将被写入spring.factories中作为自动配置类在后面的测试springBoot工程启动中被扫描到IOC容器该配置类将把上面的service服务实例化

package com.example.hello.config;
import com.example.hello.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnMissingBean(HelloService.class)
public class TestAutoConfiguration {

    @Bean
    public HelloService makeHelloService(){
        return new HelloService();
    }
}

spring.factories配置

在\resources\META-INF下创建spring.factories文本指定当前测试stater包中的配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.hello.config.TestAutoConfiguration

工程目录如下 

 本地包上传

以上就完成业务逻辑使用pom的clean+install命令把该starter工程上传本地其他程序可以直接通过pom引入

使用自定义starter依赖

再创建1个子模块test-my-project作为真正的依赖上面的业务代码实体它是个web应用

 编辑其pom依赖如下这里的重点是test01-spring-boot-starter的依赖工程是我们上面本地上传的包

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.3.7.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.example</groupId>
			<artifactId>test01-spring-boot-starter</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

测试和配置

当前工程的application.properties写入以下配置

test.hello.prefix = prefix
test.hello.suffix = suffix

直接使用注解使用starter包中的HelloService这个bean它将在启动时候被IOC实例化

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayHello(){
        return helloService.sayHello();
    }
}

输出正常表明测试的starter包被成功引入并且实现了业务代码的类被IOC实例化在真实的工程中可以直接调用。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring

“SpringBoot实践(三十八):自定义spring-boot-starter” 的相关文章