SpringBoot-注解@EnableAutoConfiguration自动装配原理
参考文献
SpringBoot 自动配置启动流程 为什么没有执行selectImports方法
@EnableAutoConfiguration注解
123456789101112131415161718192021222324252627282930@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage// 入口@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration { /** * Environment property that can be used to override when auto-configuration is * enabled. */ String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautocon ...
SpringBoot-注解@ConfigurationProperties
参考文献
@ConfigurationProperties注意点
@ConfigurationProperties
@ConfigurationProperties 通过setter和构造器来设置field的值, 并且支持级联绑定属性
配置文件中的属性个数可以和POJO中的field个数不一样, 如果这样那么field就使用默认值或是初始值
要想使@ConfigurationProperties生效, 可以通过@Component, 或是在配置类上使用@EnableConfigurationProperties, 并且指定value来使properties文件的值自动注入到对应pojo的属性中(同时会将该POJO注入到IoC)
@ConfigurationProperties 不能与@EnableConfigurationProperties 一起标注在同一个类上
可以在@ConfigurationProperties标注的类上使用@Validated在来校验field的绑定
如果一个类标明了@ConfigurationProperties, 不会对内部类生效, 需要另外配置(因 ...
SpringBoot-注解@NestedConfigurationProperty
参考文献
示例
依赖
1implementation 'org.springframework.boot:spring-boot-configuration-processor:2.2.2.RELEASE'
JavaConfig:
12345678910111213141516import com.holelin.config.oss.AliYunConfigimport com.holelin.config.oss.MinioConfigimport org.springframework.boot.context.properties.ConfigurationPropertiesimport org.springframework.boot.context.properties.NestedConfigurationPropertyimport org.springframework.stereotype.Component@Component@ConfigurationProperties(prefix = "oss")open ...