日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

springboot @ConfigurationProperties注入属性流程

發布時間:2025/3/19 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot @ConfigurationProperties注入属性流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、編寫實例,只要加上ConfigurationProperties注解,就會從當前springboot的Environment中讀取配置屬笥。Environment包括bootStrap,application,application_dev等。

@Data @Configuration @ConfigurationProperties(prefix = "redisson") public class RedissonProperties {private int timeout = 3000;private String address;private String password;private int database = 0;private int connectionPoolSize = 64;private int connectionMinimumIdleSize=10;private int slaveConnectionPoolSize = 250;private int masterConnectionPoolSize = 250;private String[] sentinelAddresses;private String masterName; }

二、注入流程

1.在SPRING容器實例化BEAN對象后,會觸發

ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization方法,進行屬性值注入。這種跟@resource的依賴注入流程機制是一樣的,都是實現了BeanPostProcessor的后置處理器,進行屬性的修改,或者對象的代理封裝轉換等。 org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {bind(ConfigurationPropertiesBean.get(this.applicationContext, bean, beanName));return bean;}

?2.隨后會調用ConfigurationPropertiesBean.create方法,判斷BEAN類上是否有ConfigurationProperties注解,有才進行配置文件的屬性注入。

private static ConfigurationPropertiesBean create(String name, Object instance, Class<?> type, Method factory) {ConfigurationProperties annotation = findAnnotation(instance, type, factory, ConfigurationProperties.class);if (annotation == null) {return null;}Validated validated = findAnnotation(instance, type, factory, Validated.class);Annotation[] annotations = (validated != null) ? new Annotation[] { annotation, validated }: new Annotation[] { annotation };ResolvableType bindType = (factory != null) ? ResolvableType.forMethodReturnType(factory): ResolvableType.forClass(type);Bindable<Object> bindTarget = Bindable.of(bindType).withAnnotations(annotations);if (instance != null) {bindTarget = bindTarget.withExistingValue(instance);}return new ConfigurationPropertiesBean(name, instance, annotation, bindTarget);}

?

3.真正讀取配置文件屬性并綁定設置到BEAN對象屬性中是由?

ConfigurationPropertiesBinder.bind方法完成。 BindResult<?> bind(ConfigurationPropertiesBean propertiesBean) {Bindable<?> target = propertiesBean.asBindTarget();ConfigurationProperties annotation = propertiesBean.getAnnotation();BindHandler bindHandler = getBindHandler(target, annotation);return getBinder().bind(annotation.prefix(), target, bindHandler);}

4.隨后會調用org.springframework.boot.context.properties.bind.binder.bindDataObject 進行數據對

象綁定。

private Object bindDataObject(ConfigurationPropertyName name, Bindable<?> target, BindHandler handler,Context context, boolean allowRecursiveBinding) {if (isUnbindableBean(name, target, context)) {return null;}Class<?> type = target.getType().resolve(Object.class);if (!allowRecursiveBinding && context.isBindingDataObject(type)) {return null;}DataObjectPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(name.append(propertyName),propertyTarget, handler, context, false, false);return context.withDataObject(type, () -> {for (DataObjectBinder dataObjectBinder : this.dataObjectBinders) {Object instance = dataObjectBinder.bind(name, target, context, propertyBinder);if (instance != null) {return instance;}}return null;});}

5.其中有個JAVA數據對象,會遍歷當前對象的所有屬性,逐個屬性進行讀取配置,進行綁定。

org.springframework.boot.context.properties.bind.JavaBeanBinder private <T> boolean bind(DataObjectPropertyBinder propertyBinder, Bean<T> bean, BeanSupplier<T> beanSupplier,Context context) {boolean bound = false;for (BeanProperty beanProperty : bean.getProperties().values()) {bound |= bind(beanSupplier, propertyBinder, beanProperty);context.clearConfigurationProperty();}return bound;}

6.最終會調用Binder.findProperty進行屬性查找。這里面的context.getSource就是springboot初始化時加載的各類資源配置屬性對象列表。包括系統環境變量,配置文件,JVM參數等。

private ConfigurationProperty findProperty(ConfigurationPropertyName name, Context context) {if (name.isEmpty()) {return null;}for (ConfigurationPropertySource source : context.getSources()) {ConfigurationProperty property = source.getConfigurationProperty(name);if (property != null) {return property;}}return null;}

7.綁定完的屬性注入如下,可以看到配置的屬性已經注入進去。

總結

以上是生活随笔為你收集整理的springboot @ConfigurationProperties注入属性流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。