SpringBoot 遗忘后的简单快速回忆之自动配置 和自定义属性注入(在最后)

SpringBoot的自动配置

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
springboot=spring+springmvc
而SpringBoot之所有这么秀,很大原因来源于它的自动配置

SpringBoot 项目的开始是由入口类开始的,所以我们从入口类开始探索
在这里插入图片描述
这里我们可以看到入口类的一个很主要的标识注解@SpringBootApplication
让我们跟随鼠标点进去看看它又何神秘之处那
在这里插入图片描述
进来之后映入眼帘的这些,有三个注解比较重要:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
来让我们依次点进去来掀开他们的面纱
在这里插入图片描述
@SpringBootConfiguration:我们点进去以后可以发现底层是Configuration注解,说白了就是支持JavaConfig的方式来进行配置(使用Configuration配置类等同于XML文件)。
@EnableAutoConfiguration (开启自动配置功能) 这个那看意思我们就知道是重头戏所以压轴,后面详解!
·@ComponentScan·:这个注解,我们对它不会陌生,就是扫描注解,默认是扫描当前类下的package。将@Controller/@Service/@Component/@Repository等注解加载到IOC容器中。

来 前菜完了,让我们来上重头戏 @EnableAutoConfiguration

先进去
在这里插入图片描述

我们可以发现有两个比较重要的注解
@AutoConfigurationPackage:自动配置包
@Import:给IOC容器导入组件

补充下@Import

将组件交由工厂管理方式有几种?
java配置   @Confirguation @Bean
注解扫描    @ComponentScan  扫描加有@Repository @Component @Service @Controller的组件
@Import    也可以将组件交由工厂管理   注意:使用@Import注解交由工厂管理的对象名称为全限定名.
           第一种使用方式  @Import(value = {Black.class,Pink.class})
           第二种使用方式  实现ImportSelector接口和其中方法  
 实现ImportSelector接口和其中方法
 @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//       逻辑代码
        String[] strs={"com.baizhi.conf.Blue","com.baizhi.conf.Red"};
        return strs;
    }

可以看的出来重点是在@AutoConfigurationPackage这个注解上,网上把它解释成自动配置包

那我们点进去看看
在这里插入图片描述
我们点进去又看到了@Import 那就点Registrar进去查看
在这里插入图片描述
我们可以发现这个代码比较重要!
在默认的情况下就是将:主配置类(@SpringBootApplication)的所在包及其子包里边的组件扫描到Spring容器中。
它看着有点和ComponentScan 重复 其实还是有区别的
比如说,你用了Spring Data JPA,可能会在实体类上写@Entity注解。这个@Entity注解由@AutoConfigurationPackage扫描并加载,而我们平时开发用的@Controller/@Service/@Component/@Repository这些注解是由ComponentScan来扫描并加载的。
简单理解:这二者扫描的对象是不一样的

不过这好像和自动配置没多大关系,那让我们回到前前面的@Import(AutoConfigurationImportSelector.class)AutoConfigurationImportSelector 进去瞧瞧
在这里插入图片描述

getCandidateConfigurations 翻译一下的大概意思是获得很多候选人配置 让我们点进去看看它
在这里插入图片描述
SpringFactoriesLoader.loadFactoryNames (Sparing工厂装载 . 加载的一些工厂名) 我们继续进
在这里插入图片描述
简单的说
Spring启动的时候会扫描所有jar路径下的META-INF/spring.factories,将其文件包装成Properties对象
从Properties对象获取到key值为EnableAutoConfiguration的数据,然后添加到容器里边。
在这里插入图片描述
筛选出以EnableAutoConfiguration 为key 的信息

可以在这在这里插入图片描述里翻翻哦
在这里插入图片描述

总述

@SpringBootApplication等同于下面三个注解:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
其中@EnableAutoConfiguration是关键(启用自动配置),内部实际上就去加载META-INF/spring.factories文件的信息,然后筛选出以EnableAutoConfiguration为key的数据,加载到IOC容器中,实现自动配置功能!

其实我们是可以自定义属性注入的

1.配置文件中声明相关属性

比如:
application.yml
server.port=8989
server.context-path=/springboot
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/springboot
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root

2.创建属性配置类并对属性进行注入

@ConfigurationProperties(prefix = "jdbc")  //读取application.yml的属性,并且为当前属性类添加前缀 
public class JDBCProperties {
    private String username;
    private String password;
    private String url;
    private String driver;
}

3.创建配置类引入属性配置类

@EnableConfigurationProperties(value = JDBCProperties.class)//获取配置文件类 
@Configuration
public class JDBCConf {
    @Autowired
    JDBCProperties jdbcProperties;

     @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setPassword(jdbcProperties.getPassword());
        druidDataSource.setUsername(jdbcProperties.getUsername());
        druidDataSource.setUrl(jdbcProperties.getUrl());
        druidDataSource.setDriverClassName(jdbcProperties.getDriver());
        return druidDataSource;
    }
}

补充:此jar包可以让配置文件有提示

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
发布了8 篇原创文章 · 获赞 4 · 访问量 211

猜你喜欢

转载自blog.csdn.net/weixin_46001623/article/details/105034868