目录
自动配置原理
基于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实例化,在真实的工程中可以直接调用。
转载:https://blog.csdn.net/yezonggang/article/details/128646349