依赖的管理

对于所有的依赖我们需要在父项目中设置版本号等都设置好了,在子项目中就不需要再配置版本

1
2
3
4
5
6
7
8
9
10
11
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
他的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>

但是如果想要使用非SpringBoot官方引入的jar的话就需要写版本号

starter场景启动器

  1. 很多spring-boot-starter : 就某种场景
  2. 只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
  3. SpringBoot所有支持的场景
  4. -spring-boot-starter: 第三方为我们提供的简化开发的场景启动器

自动配置

自动配置好Tomcat ,SpringMVC , SpringMVC中的常见组件 ,配置好了web的常见功能 。

@SpringBootApplication 注解的拆分 :

是由下面几个注解合成

  • @SpringBootConfiguration:·标识该类是 Spring Boot 的配置类,用于定义配置信息。

  • @EnableAutoConfiguration:开启自动配置功能,Spring Boot 会自动根据项目中的依赖和配置信息,来对 Spring 进行自动配置;

  • @ComponentScan:扫描指定包及其子包下的所有组件,包括 @Component、@Controller、@Service 等。

使用 @ComponentScan(“包名”) 可以改变扫描路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
在之前我们使用的@SpringBootApplication ,他只能扫描自己所在包里面的所有的组件
使用@ComponentScan("包名") 和下面的另外两个就可以代替@SpringBootAplication
// @SpringBootConfiguration }
// @EnableAutoConfiguration } == 》 @SpringBootApplication
// @ComponentScan("com.atguigu.boot") }

public class{
public static void main(String[] args){
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}

//3、从容器中获取组件

Pet tom01 = run.getBean("tom", Pet.class);

}

}

各种配置拥有默认值

  • 默认配置最终都是映射到某个类上,如:MultipartProperties
  • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象

按需加载所有自动配置项

    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面

![a]/images/67f5837cc4c54699a7401fa9a6fa89dftplv-k3u1fbpfcp-zoom-in-crop-mark3024000.webp)

容器管理

原生的spring添加组件的方法:

创建一个spring.xml的类,然后配置组件的信息,注册组件

img

SpringBoot中添加组件的方法:

  1. 创建一个类,将这个类设置为配置类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Configuration		//用来告诉SpringBoot 这是一个配置文件类	
//@Configuration(proxyBeanMethods = false) //设置多实例对象
public class MyConfig {

@Bean
public User user1(){
/*给容器中添加组件,一方法名作为组件的id , 返回类型就是组件类型 , 值就是组件中的内容实例*/
return new User("zhansan",12);
}
//等同于下面的
/**
<bean id="user1" class="com.ray.bean.User">
<property name="name" value="da"/>
<property name="age" value="11"/>
</bean>
*/
@Bean("tom") // 也可以直接在bean中设置自定义的组件名 == 组件的id
public Pet tomcatPet(){
return new tomcat("tom" , 11);
}

}
  1. 给方法名设置@Bean注解,将方法名当做组件的id ,返回类型就是组件类型 ,返回值就是组件在容器中的实例(对象)

  2. @Configuration(proxyBeanMethods = false) // 中默认的是true 。

    • 值为true 那么无论我们调用多少次组件,他返回的都是单例的。【每次调用都是获取容器中唯一的】

    • 值为false :【 那么我们每个@Bean方法被调用多少次返回多少个新的组件

  3. 同时我们定义的Config配置类,他也是容器中的一个组件

其他组件

@Bean、@Component、@Controller、@Service、@Repository(数据库组件)

@ComponentScan(指定包扫描的)、@Import(放在任何一个组件上面都行)

@Import注解

@Import({ XXX.class, AAA.class ….} ) 组件

作用:给容器导入组件,他是一个数组类型的

可以自动的给容器中创建调用这其中组件的无参构造器 ,从而创建出指定类型的对象 [默认组件的名字就是全类名]

@Conditional

条件装配组件(如果条件满足或者说如果条件不满足才执行XXX组件)

条件组件,就是当我们的类中有名为XXX的时候,我们类中的XXX组件才会被执行,才会生效

1
2
3
4
5
6
7
8
9
10
11
12
13
@ConditionalOnBean(name = "tom")	
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}

@Bean("tom22")
public Pet tomcatPet(){
return new Pet("tomcat");
}

上述代码的得到的结果是

image-20230406195125886

如果将条件组件标记在类上,那么就是除非类中有tom组件,配置配种的其他组件才会被执行

1
2
3
4
5
6
7
8
9
@Configuration		//用来告诉SpringBoot 这是一个配置文件类	
//@Configuration(proxyBeanMethods = false) //设置多实例对象
@Conditional(name = "tom") //就是说当容器中由tom名的组件的时候 ,下面的配置内容才生效
public class MyConfig {
@Bean("tom22"){
return new Tom(XXX);
}
}

得到的结果是

image-20230406195439690

其他条件装配注解

img

将xml文件中的内容注入到容器

要么一点一点自己转换, 要么像下面那样直接进行解析

利用注解 :@ImportResource

![image-20230406195824922]/images/image-20230406195824922.png)

配置绑定

以前 ,我们习惯使用配置文件进行配置内容, 但是随着项目的更迭 ,就需要使用SpringBoot进行维护开发…所以就需要将原来的配置文件直接导入到现有的项目中。

如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用

1
2
3
4
5
6
7
8
9
10
11
12
13
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
}
}
}
  1. @ConfigurationProperties
    @Component只有在容器中的组件,才会拥有SpringBoot提供的强大功能
    @ConfigurationProperties(prefix = “mycar”) //将类放入容器中(@Compnent),然后与javaBean (mycar)与配置绑定

img

img

1
2
3
4
5
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
  1. 开启属性配置功能

@EnableConfigurationProperties (在配置类中) + @ConfigurationProperties(在javaBean类中) = 就是开启属性配置,然后将配置与javaBean绑定

1
2
3
4
5
6
7
8
9
10
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
}

//在Car类中就不需要使用@Component注解

@ConfigurationProperties(prefix = "mycar")
public class Car {

自动装配源码解析

  1. 引导加载自动配置类

**@**SpringBiootApplication注解

  • @SpringBootConfiguration

  • @ComponentScan : 作用是指定扫描哪些注解

  • @EmableAutoConfiguration最重要的注解

img

@EmableAutoConfiguration由下面两个个注解的合成。

  • AutoConfiguationPackage : 自动配置包

img

  • @Import(AutoConfigurationImportSelector.class): 作用:给容器导入一个组件Registrar
1
2
3
4
5
6
1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
2、调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
3、利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件
4、从META-INF/spring.factories位置来加载一个文件。
默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
  1. spring.factorites 中有127个场景配置,也就是说springBoot一启动就会加载所有的配置类

  2. 但是按照条件装配规则(@Conditional),最终会按需加载

按需开启自动配置项

只有(@Conditional)生效之后,所在的配置类才会被加载进容器,然后生效

  • SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration

  • 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。所有的值都是从xxxxProperties里面拿。xxxProperties和配置文件进行了绑定

  • 生效的配置类就会给容器中装配很多组件

  • 只要容器中有这些组件,相当于这些功能就有了

  • 定制化配置

    • 用户直接自己@Bean替换底层的组件
    • 用户去看这个组件是获取的配置文件什么值就去修改。

xxxxxAutoConfiguration(导入进去) —> 容器中就会有这些组件 —> 组件又从xxxxProperties里面拿值(然后它从后面的那个里面拿值) —-> application.properties

参考说明: 尚硅谷雷神SpringBoot