当前位置:网站首页>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
边栏推荐
- webService接口
- How does MySql find out the latest data row that meets the conditions?
- GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
- [Linear table] - Detailed explanation of three practice questions of LeetCode
- The Azure developer news 丨 memorabilia in July
- DAY17, CSRF vulnerability
- Unity3D Application simulation enters the front and background and pauses
- Thymeleaf简介
- protobuf 中复合数据类型的读写
- 厦门感芯科技MC3172(1):介绍和环境搭建
猜你喜欢
随机推荐
golang八股文整理(持续搬运)
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
MNIST of Dataset: MNIST (handwritten digital image recognition + ubyte.gz file) data set introduction, download, usage (including data enhancement) detailed guide
Code open source design and implementation ideas
1. Get data - requests.get()
MySQL 操作语句大全(详细)
Azure 开发者新闻快讯丨开发者7月大事记一览
山西省第二届网络安全技能大赛(企业组)部分赛题WP(七)
双指针问题(下)
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
《构建之法》笔记---第十章 典型用户和场景
共建共享数字世界的根:阿里云打造全面的云原生开源生态
山西省第二届网络安全技能大赛(企业组)部分赛题WP(八)
JQ source code analysis (environment)
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!
"Translation" Envoy Fundamentals, this is a training course, make people to more quickly using Envoy Proxy..
Weight line segment tree + line segment tree split/merge + CF1659D
Become a qualified cybersecurity, do you know this?