springboot历史:
image.png image.png Screenshot_20210705_190816_tv.danmaku.bili.jpg
springboot给属性赋值的几种方式,可以通过yaml也可以通过propertities
还可以用@PropertitySource(value="xxx")
@ConfigurationPropetities(prefix="xx")此时将yaml中前缀为xx的属性的值注入到了这个容器组件中
除了使用@ConfigurationProperties注入属性的值,还可以使用Spring的底层注解@Value注入属性的值。
@Component
public class Person {
@Value("${person.last-name}")
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
}
那么这两个注解的区别是什么?
@ConfigurationProperties能够批量注入配置文件的属性。
@Value只能一个个指定。
@ConfigurationProperties支持松散绑定。
@ConfigurationProperties(prefix = "person"),只需要指定一个前缀,就能绑定有这个前缀的所有属性值。
@ConfigurationProperties还支持JSR303进行配置文件值及校验。
image.png
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value,如果我们转么编写了一个javabean和配置文件进行映射,就直接使用ConfigurationProperties;
image.png Screenshot_20210705_190957_tv.danmaku.bili.jpg@SpringBootApplication:
image.png Screenshot_20210705_193414_tv.danmaku.bili(1).jpgspring security:
image.png Screenshot_20210705_154906_tv.danmaku.bili.jpg
自动配置的和核心文件
spring factories
image.png 137030616254852752.png image.png Screenshot_20210705_195040_tv.danmaku.bili.jpg image.png Screenshot_20210705_195157_tv.danmaku.bili.jpg
image.png Screenshot_20210705_200523_tv.danmaku.bili.jpg
配置类@Configuration也是容器中的一个组件
@EnbaleautoConfiguration 以前需要自己配置的东西springboot帮我们自动配置,这个注解用于告诉springboot开启自动配置功能,这样自动配置才能生效
@ComponentScan:相当于在spring.xml中配置<context:comonent-scan>但是并没有指定basepackage,其中(excludeFilters用来表示排除某些Bean,可以用某种规则来排除某些bean,在spring-ioc中是这样配置的:
@EnableAutoConfiguration:
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
@AutoConfigurationPackage
将当前配置类所在包保存在BasePackages的Bean中。供Spring内部使用
@AutoConfigurationPackage
@Import(AutoConfigurationPackages.Registrar.class) // 保存扫描路径, 提供给spring‐data‐jpa 需要扫描 @Entity
public @interface AutoConfigurationPackage {
@Import(EnableAutoConfigurationImportSelector.class) 关键点!
可以看到,在@EnableAutoConfiguration注解内使用到了@import注解来完成导入配置的功能,而EnableAutoConfigurationImportSelector 实现了DeferredImportSelectorSpring内部在解析@Import注解时会调用getAutoConfigurationEntry方法,这块属于Spring的源码,有点复杂,我们先不管它是怎么调用的。
下面是2.3.5.RELEASE实现源码:
getAutoConfigurationEntry方法进行扫描具有META-INF/spring.factories文件的jar包。
视图解析器:
image.png Screenshot_20210705_201708_tv.danmaku.bili.jpg
在spring中的对象注入的方法
image.png Screenshot_20210706_135913_tv.danmaku.bili.jpg注意,一定要提供set方法,否则无法成功
image.png Screenshot_20210706_141417_tv.danmaku.bili.jpg
ref用于javabean对象,value用于基本类型
网友评论