SpringBoot笔记(2)

一、容器功能

1.1 组件添加

1. @Configuration

  1. Full模式:获取对象时,首先在容器内搜索是否存在,如存在直接拿出
  • 默认为Full模式,单例
  • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
@Configuration(proxyBeanMethods = true)
  1. Lite模式:获取对象时,直接创建新对象
  • 多例
  • 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
@Configuration(proxyBeanMethods = false)

配置类编写

  • @Configuration
  • @Bean
//告诉SpringBoot这是一个配置类
@Configuration
public class MyConfig {
    @Bean
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        zhangsan.setPet(pet01());
        return zhangsan;
    }
    @Bean
    public Pet pet01(){
        Pet tom = new Pet("tom");
        return tom;
    }
}

2. @Bean、@Component、@Controller、@Service、@Repository

3. @ComponentScan、@Import

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}

给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名

4. @Conditional

条件装配:满足Conditional指定的条件,则进行组件注入

由于User依赖Pet,所以当pet组件不存在时,user组件也没有意义,所以使用条件装配@ConditionalOnBean

//存在pet01组件则进行组件注入
@ConditionalOnBean(name = "pet01")
@Bean
public User user01(){
    User zhangsan = new User("zhangsan", 18);
    zhangsan.setPet(pet01());
    return zhangsan;
}

1.2 原生配置文件引入

1. @ImportResource

作用:已经在xml中配置了组件,让配置文件中的内容生效

@ImportResource("classpath:beans.xml")
public class MyConfig {}

1.3 配置绑定@ConfigurationProperties

类似于jdbc的xml中数据绑定,可以使用@ConfigurationProperties

方法一:@EnableConfigurationProperties + @ConfigurationProperties

步骤:

  1. 在MyConfig中添加@EnableConfigurationProperties
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
}
  1. 在pojo的实体类上添加@ConfigurationProperties
mycar.name=BYD
mycar.price=1000

​ prefix表示xml文件中的前缀

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

方法二:@Component + @ConfigurationProperties

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

二、自动配置原理入门

2.1 引导加载自动配置类(注解)

@SpringBootApplication = @SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

1. @SpringBootConfiguration

@Configuration。代表当前是一个配置类

2. @ComponentScan

指定扫描哪些,Spring注解;

3. @EnableAutoConfiguration

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
  • @AutoConfigurationPackage:

    ​ 自动配置包?指定了默认的包规则

  • @Import(AutoConfigurationImportSelector.class)

    1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
    2、调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
    3、利用工厂加载 Map<String, List> 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

文件里面写死了spring-boot一启动就要给容器中加载的所有配置类

2.2 按需开启自动配置项

虽然127个自动配置启动时都加载了(xxxAutoConfiguration),但会按照条件装配规则@Conditional,进行按需装配

2.3 修改用户的配置

给容器中加入了文件上传解析器;

@Bean
@ConditionalOnBean(MultipartResolver.class)  //容器中有这个类型组件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
//给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范
// Detect if the user has created a MultipartResolver but named it incorrectly
return resolver;
}

将用户的不规范的命名进行重命名

2.4 总结

  • SpringBoot首先会加载所有的自动配置类
  • 每个自动配置类按条件进行生效
  • 大多数自动配置类默认会绑定配置文件
  • 生效的配置类就会给容器中装配很多组件
  • SpringBoot默认会在底层配好所有的组件。但是如果用户自己配置了以用户的优先

顺序:

首先加载自动配置类------>按条件生效的自动配置类----->寻找配置文件------->根据配置文件的数据为容器添加组件(用户优先)

  • 定制化配置

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

三、最佳应用

四、开发小技巧

4.1 lombok

==================导入依赖===============
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
  1. 简化javaBean开发
=================添加注解=================
@NoArgsConstructor//无参构造
@AllArgsConstructor//全参数构造
@Data//引入get,set,toString方法
public class User {
    private String name;
    private int age;
    private Pet pet;
}
  1. 简化日志开发
@Slf4j
public class HelloController {
    @RequestMa

4.2 dev-tools

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

项目或者页面修改以后:Ctrl+F9;

4.3 Spring Initailizr(项目初始化向导)

会自动进行依赖导入,自动创建项目结构,自动配好主配置类

posted @ 2021-08-26 19:24  橡皮筋儿  阅读(42)  评论(0编辑  收藏  举报