当前位置:网站首页>3. Dependency configuration management
3. Dependency configuration management
2022-07-30 04:37:00 【Time the postman】
1、依赖管理
1)父项目(继承依赖,声明了各种jarPackage dependency version number)
几乎声明了所有开发中常用的依赖的版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
</parent>
2)Custom dependency version number(pom.xml)
1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key.
2、在当前项目里面重写配置
<properties>
<mysql.version>8.0.26</mysql.version>
</properties>
3)Starters场景启动器
1、will see a lot spring-boot-starter-* : *就是某种场景
2、只要引入starter,这个场景的所有常规需要的依赖都会自动引入
3、SpringBoot所有支持的场景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
4、见到的 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器.
5、所有场景启动器最底层的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.2</version>
<scope>compile</scope>
</dependency>
2、自动配置
自动配好Tomcat(依赖/配置)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.2</version>
<scope>compile</scope>
</dependency>
自动配好SpringMVC(引入MVC全套组件/Automatically configure its common components)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
<scope>compile</scope>
</dependency>
自动配好Web常见功能,如字符编码问题(所有web开发的常见场景)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.22</version>
<scope>compile</scope>
</dependency>
默认包结构
All components in packages and subpackages at the same level of the main program are scanned by default,可用@SpringBootApplication(scanBasePackages=“com.yzh”)改变扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.yzh.boot")
各种配置都有默认值(默认配置最终都是映射到MuitipartProperties)
按需加载所有自动配置项(根据场景Starter引入)
3、容器功能
1)组件添加
@Configuration
Full模式与Lite模式
package com.yzh.boot.config;
import com.yzh.boot.bean.Pet;
import com.yzh.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的 * 2、配置类本身也是组件 * 3、proxyBeanMethods:代理bean的方法 * Full(proxyBeanMethods = true) 【保证每个@Bean方法返回的组件都是单实例的】 * Lite(proxyBeanMethods = false)【每个@Bean方法返回的组件都是新创建的】 * 组件依赖必须使用Full模式. */
@Configuration(proxyBeanMethods = true) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
@Bean //给容器中添加组件.以方法名作为组件的id 返回类型就是组件类型 返回的值,就是组件在容器中的实例
public User user01(){
User user = new User("叶子航", 20,tomcatPet());
//User组件依赖了Pet组件
user.setPet(tomcatPet());
return user;
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
@Import
@Import(xxx.class,xxx.class):给容器中自动创建出这两个类型的组件
@Import({
User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}
@Conditional
条件装配(Meet the specified assembly,则进行组件注入)
package com.yzh.boot.config;
import ch.qos.logback.classic.db.DBHelper;
import com.yzh.boot.bean.Pet;
import com.yzh.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/** * 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的 * 2、配置类本身也是组件 * 3、proxyBeanMethods:代理bean的方法 * Full(proxyBeanMethods = true) 【保证每个@Bean方法返回的组件都是单实例的】 * Lite(proxyBeanMethods = false)【每个@Bean方法返回的组件都是新创建的】 * 组件依赖必须使用Full模式. */
@Import({
User.class}) //给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
@Configuration(proxyBeanMethods = true) //告诉SpringBoot这是一个配置类 == 配置文件
//@ConditionalOnBean(name = "tom") If the component does not exist in the container,Then all the following components will not take effect
public class MyConfig {
@Bean(name="tom")
public Pet pet(){
return new Pet("tomcat");
}
@Bean //给容器中添加组件.以方法名作为组件的id 返回类型就是组件类型 返回的值,就是组件在容器中的实例
@ConditionalOnBean(name = "tom")
public User user01(){
User user = new User("叶子航", 20,pet());
return user;
}
}
2)原生配置文件引入
@ImportResource
将xml创建的bean加入容器
<bean id="user" class="com.yzh.boot.bean.User"></bean>
@ImportResource("classpath:beans.xml")
3)配置绑定
@Component + @ConfigurationProperties
Configure the property value of the configuration file configuration to createdbean并加入到容器
package com.yzh.boot.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix ="mycar" )
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String brand;
private Integer price;
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
mycar.brand="宝马"
mycar.price=100

@EnableConfigurationProperties + @ConfigurationProperties
将指定的bean注入容器
4、自动配置原理
@SpringBootApplication
@SpringBootConfiguration
@Configuration(Indicates that it is a configuration class)
@EnableAutoConfiguration
@AutoConfigurationPackage(自动配置包)
@Import({Registrar.class}) (批量注册,Registrar.class见下图)
@Import({AutoConfigurationImportSelector.class}) (Configurations are fully loaded but allocated on demand 见下图)
@ComponentScan(包扫描,指定扫描)
1)引导加载自动配置类
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
Registrar() {
}
//Get the package name of the main program and register all components under its package to the container
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
}
2)按需开启自动配置项
@Import({
AutoConfigurationImportSelector.class})
虽然我们127个场景的所有自动配置启动的时候默认全部加载.xxxxAutoConfiguration
按照条件装配规则(@Conditional),最终会按需配置.
3)修改默认配置
SpringBoot默认会在底层配好所有的组件,但是如果用户自己配置了以用户的优先
SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration,每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值
xxxProperties和配置文件进行了绑定,生效的配置类就会给容器中装配很多组件,只要容器中有这些组件,相当于这些功能就有了
定制化配置-----用户直接自己@Bean替换底层的组件,用户去看这个组件是获取的配置文件什么值就去修改
xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties
边栏推荐
- labelme的使用技巧
- MySQL String Concatenation - Various String Concatenation Practical Cases
- 数据目录是什么?为何需要它?
- 05 Detailed explanation of the global configuration file application.properties
- [Linear table] - Detailed explanation of three practice questions of LeetCode
- Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
- Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b
- Introduction to database - MySQL simple introduction
- 2021 Shandong Province Network Construction and Application Test Questions
- 双指针问题(下)
猜你喜欢

我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福

Repetition XXL - JOB scheduling center background arbitrary command execution

Simple experiment with BGP

2.6归并排序

【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?

Machine Learning: Knowing the Dimensionality Reduction Process Through Low Variance Filtering

Introduction to database - MySQL simple introduction

golang八股文整理(持续搬运)

How does MySql find out the latest data row that meets the conditions?

Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
随机推荐
2021山东省网络搭建与应用赛项试题
Simple experiment with BGP
2.6基数排序(桶排序)
Thymeleaf简介
Classification of decision tree classification
Go study notes (84) - Go project directory structure
unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
How does MySql find out the latest data row that meets the conditions?
What is the data directory?Why do you need it?
2.5 Quick Sort
双指针问题(中)
小程序 wx.miniProgram.navigateTo 跳转地址不能是tabbar地址
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
swagger使用教程——快速使用swagger
【周周有奖】云原生编程挑战赛“边缘容器”赛道邀你来战!
The VUX Datetime component compute-days-function dynamically sets the date list
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
How with Mexico Volkswagen VW EDI connection to Mexico
【MySQL系列】-B+树索引和HASH索引有什么区别
state space representation