当前位置: 首页 > news >正文

济南市建设局网站查房产信息头条网站怎么做

济南市建设局网站查房产信息,头条网站怎么做,外贸网站推广平台排名,dw做的网站上传PropertySourcesBeanFactoryPostProcessor详解 1. 核心概念 BeanFactoryPostProcessor 是 Spring 框架中用于在 BeanFactory 初始化阶段 对 Environment 中的 PropertySource 进行后处理的接口。它允许开发者在 Bean 创建之前 对属性源进行动态修改,例如添加、删除…

PropertySourcesBeanFactoryPostProcessor详解

在这里插入图片描述


1. 核心概念

BeanFactoryPostProcessor 是 Spring 框架中用于在 BeanFactory 初始化阶段Environment 中的 PropertySource 进行后处理的接口。它允许开发者在 Bean 创建之前 对属性源进行动态修改,例如添加、删除或转换属性。


2. 核心流程与类关系
2.1 核心接口与实现
  • 接口定义

    public interface PropertySourcesBeanFactoryPostProcessor {void postProcessPropertySources(ConfigurableListableBeanFactory beanFactory, MutablePropertySources propertySources);
    }
    
    • 参数
      • ConfigurableListableBeanFactory:Spring 的 BeanFactory 实例。
      • MutablePropertySources:可修改的属性源集合(包含所有已加载的 PropertySource)。
  • 作用
    在 BeanFactory 初始化过程中,提供对 PropertySource 的直接修改能力,例如:

    • 动态添加自定义属性源(如从数据库读取配置)。
    • 过滤敏感属性(如密码)。
    • 调整属性源的优先级。

3. 典型使用场景
3.1 动态添加 PropertySource
// 示例:从数据库加载配置并添加到 PropertySources
public class DatabasePropertySourcePostProcessor implements PropertySourcesBeanFactoryPostProcessor {@Overridepublic void postProcessPropertySources(ConfigurableListableBeanFactory beanFactory, MutablePropertySources propertySources) {// 1. 从数据库获取配置Map<String, Object> dbProps = loadConfigFromDatabase();// 2. 创建自定义 PropertySourcePropertySource<?> dbPropertySource = new MapPropertySource("databaseConfig", dbProps);// 3. 将其添加到最高优先级(覆盖现有配置)propertySources.addFirst(dbPropertySource);}
}
3.2 过滤敏感属性
public class SensitivePropertyFilter implements PropertySourcesBeanFactoryPostProcessor {@Overridepublic void postProcessPropertySources(ConfigurableListableBeanFactory beanFactory, MutablePropertySources propertySources) {// 遍历所有 PropertySource 并过滤敏感键List<PropertySource<?>> filteredSources = new ArrayList<>();for (PropertySource<?> source : propertySources) {if (source instanceof EnumerablePropertySource) {Map<String, Object> filteredProps = new HashMap<>();for (String key : ((EnumerablePropertySource<?>) source).getPropertyNames()) {if (!key.contains("password")) {filteredProps.put(key, source.getProperty(key));}}PropertySource<?> filteredSource = new MapPropertySource(source.getName(), filteredProps);filteredSources.add(filteredSource);}}// 替换原有的 PropertySourcespropertySources.clear();propertySources.addAll(filteredSources);}
}
3.3 调整属性源优先级
public class CustomPropertyOrderPostProcessor implements PropertySourcesBeanFactoryPostProcessor {@Overridepublic void postProcessPropertySources(ConfigurableListableBeanFactory beanFactory, MutablePropertySources propertySources) {// 将某个 PropertySource 移动到最高优先级PropertySource<?> sourceToMove = propertySources.get("customConfig");if (sourceToMove != null) {propertySources.remove("customConfig");propertySources.addFirst(sourceToMove);}}
}

4. 实现步骤
4.1 定义处理器
public class CustomPropertyProcessor implements PropertySourcesBeanFactoryPostProcessor {@Overridepublic void postProcessPropertySources(ConfigurableListableBeanFactory beanFactory, MutablePropertySources propertySources) {// 在此处实现属性源的修改逻辑}
}
4.2 注册处理器

通过 @Bean 注册到 Spring 容器:

@Configuration
public class AppConfig {@Beanpublic PropertySourcesBeanFactoryPostProcessor customProcessor() {return new CustomPropertyProcessor();}
}
4.3 集成到 Spring Boot

在 Spring Boot 中,可以通过 EnvironmentPostProcessor 在启动时注入:

public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {@Overridepublic void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {// 在此处注册 PropertySourcesBeanFactoryPostProcessor}
}

5. 关键流程与方法
5.1 属性源处理流程
Spring Boot 启动 →加载所有 PropertySource(如 application.properties、环境变量等) →调用 PropertySourcesBeanFactoryPostProcessor.postProcessPropertySources() →修改后的 PropertySources 用于后续 Bean 的属性注入。
5.2 核心方法详解
方法说明
postProcessPropertySources()核心方法,接收 BeanFactoryMutablePropertySources,进行属性源修改。
propertySources.addFirst(source)将属性源添加到最高优先级(覆盖现有配置)。
propertySources.remove(name)移除指定名称的属性源。
propertySources.get(name)根据名称获取属性源。

6. 典型应用场景总结
场景解决方案示例代码片段
动态配置注入从数据库/远程服务加载配置并添加为 PropertySource。propertySources.addFirst(new MapPropertySource("dbConfig", dbProps));
敏感信息过滤移除或修改包含敏感信息的属性键(如密码)。if (!key.contains("password")) { ... }
优先级调整将自定义配置的优先级设为最高,覆盖默认配置。propertySources.addFirst(existingSource);
属性值转换将字符串属性转换为复杂类型(如 ListMap)。Map<String, Object> convertedProps = ...

7. 注意事项
  1. 执行时机:在 BeanFactory 初始化阶段执行,早于 @PostConstruct 和 Bean 初始化。
  2. 优先级控制:通过 addFirst()addLast()insertBefore()/insertAfter() 精确控制属性源顺序。
  3. 副作用风险:避免在处理器中执行耗时操作,可能影响应用启动速度。
  4. Spring Boot 集成:需确保处理器在 Environment 初始化后被正确调用。

8. 总结表格
功能实现方式适用场景
动态配置注入通过 addFirst() 添加自定义 PropertySource。需要从外部源(如数据库)加载配置时。
属性过滤遍历 PropertySources 并移除敏感键。隐藏敏感配置(如数据库密码、API密钥)。
优先级调整使用 addFirst()remove() 调整属性源顺序。需要高优先级配置覆盖默认值(如测试环境覆盖生产配置)。
属性值转换在处理器中修改属性值类型(如字符串转 List)。需要动态解析复杂类型配置时。

通过 PropertySourcesBeanFactoryPostProcessor,可以灵活控制属性源的加载和修改逻辑,满足复杂配置需求。

延伸阅读

Post-Processing PropertySource instance详解


1. 核心概念

PropertySource 是 Spring 框架中用于管理配置属性的抽象类,负责从不同来源(如 application.properties、环境变量、系统属性等)加载属性值。
Post-Processing 是指在 PropertySource 被创建或注册到 Environment 后,对其内容进行进一步的处理或修改。


2. 核心流程与类关系
2.1 核心类与接口
类/接口作用
PropertySource属性源的抽象基类,封装属性键值对(如 server.port=8080)。
EnvironmentSpring 的环境对象,管理所有 PropertySource 的优先级和合并逻辑。
PropertySources存储 PropertySource 集合的容器,按优先级排序。
PropertySourceProcessorPropertySource 进行后处理的接口(如过滤、转换属性)。
PropertySourcesPropertyResolver根据优先级从多个 PropertySource 中解析属性值的工具类。
2.2 核心流程
  1. 属性源加载:Spring Boot 启动时,从 application.properties、YAML 文件、环境变量等加载属性,生成多个 PropertySource 实例。
  2. 属性源注册:将所有 PropertySource 注册到 EnvironmentPropertySources 容器中。
  3. 后处理阶段:对已注册的 PropertySource 进行统一处理(如过滤敏感属性、替换占位符、合并配置等)。

3. 典型 Post-Processing 场景
3.1 属性过滤
  • 场景:隐藏敏感属性(如密码、API密钥)。
  • 实现方式
    // 自定义 PropertySourceProcessor
    public class SensitivePropertyFilter implements PropertySourceProcessor {@Overridepublic PropertySource<?> processPropertySource(PropertySource<?> source) {if (source.getName().equals("someConfig")) {Map<String, Object> filteredProps = new HashMap<>();((MapPropertySource) source).forEach((key, value) -> {if (!key.contains("password")) {filteredProps.put(key, value);}});return new MapPropertySource(source.getName(), filteredProps);}return source;}
    }
    
3.2 属性值转换
  • 场景:将字符串属性转换为其他类型(如 ListMap)。
  • 实现方式
    public class TypeConverterProcessor implements PropertySourceProcessor {@Overridepublic PropertySource<?> processPropertySource(PropertySource<?> source) {Map<String, Object> convertedProps = new HashMap<>();((MapPropertySource) source).forEach((key, value) -> {if (key.endsWith(".asList")) {convertedProps.put(key, Arrays.asList(value.toString().split(",")));} else {convertedProps.put(key, value);}});return new MapPropertySource(source.getName(), convertedProps);}
    }
    
3.3 属性覆盖与合并
  • 场景:根据优先级合并多个属性源(如 application.properties 覆盖默认配置)。
  • 实现方式
    // Spring 的默认合并逻辑由 PropertySourcesPropertyResolver 处理
    Environment env = ...;
    String value = env.getProperty("key"); // 自动按优先级合并
    

4. 自定义 Post-Processing 的实现步骤
4.1 实现 PropertySourceProcessor
public class CustomPropertyProcessor implements PropertySourceProcessor {@Overridepublic PropertySource<?> processPropertySource(PropertySource<?> source) {// 在此处修改或过滤 PropertySourcereturn source; // 返回修改后的 PropertySource}
}
4.2 注册处理器
@Configuration
public class PropertyConfig {@Beanpublic PropertySourceProcessor customProcessor() {return new CustomPropertyProcessor();}
}
4.3 集成到 Spring Boot

通过 EnvironmentPostProcessor 在启动时注入处理器:

public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {@Overridepublic void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {environment.getPropertySources().addFirst(new CustomPropertySourceProcessor().processPropertySource(...));}
}

5. 关键方法与流程
5.1 属性解析流程
Environment.getProperty(key) →PropertySourcesPropertyResolver →遍历 PropertySources(按优先级) →调用 PropertySource.getProperty(key) →返回第一个非空值
5.2 核心方法详解
方法作用
PropertySource.getProperty(key)根据键直接从当前属性源获取值。
PropertySources.getFirst(name)根据名称获取第一个匹配的属性源。
PropertySources.addFirst(source)将属性源添加到优先级最高位置(覆盖现有配置)。

6. 典型应用场景
场景解决方案
敏感属性过滤实现 PropertySourceProcessor 过滤敏感键(如 password)。
动态属性注入EnvironmentPostProcessor 中动态添加属性源(如从数据库读取配置)。
属性值类型转换使用 PropertySourceProcessor 将字符串转换为复杂类型(如 ListMap)。
多环境配置合并按优先级加载 application-{profile}.properties 并合并到 Environment。

7. 总结表格
功能实现方式适用场景
属性过滤实现 PropertySourceProcessor 过滤敏感键。隐藏敏感配置(如数据库密码)。
属性转换在处理器中修改属性值类型(如字符串转 List)。需要动态解析复杂类型配置时。
属性覆盖通过 PropertySources.addFirst() 调整属性源优先级。需要高优先级配置覆盖默认值(如测试环境覆盖生产配置)。
动态属性注入EnvironmentPostProcessor 中注册新 PropertySource。配置需从外部源(如数据库、API)动态加载时。

8. 注意事项
  1. 优先级控制:属性源的加载顺序决定了覆盖规则,需通过 PropertySources.addFirst()addLast() 明确优先级。
  2. 性能影响:复杂的后处理逻辑可能增加启动时间,需避免在高频路径中执行。
  3. Spring Boot 集成:通过 @ConfigurationEnvironmentPostProcessor 灵活扩展。

通过以上方法,可以灵活控制属性源的后处理逻辑,满足复杂配置需求。

http://www.dtcms.com/a/514772.html

相关文章:

  • 聊城网站优化网络推广怎么做垂直门户网站
  • 宁波seo关键词优化报价海淀区seo搜索引擎
  • 微信网站开发 js框架哪里可以学网站开发
  • 网站设计要多久网站开发 慕课
  • 网站建设擎宇WordPress如何去掉文章时间
  • 做网站网页维护手机App开发页面排版布局
  • 棋牌论坛网站怎么做个人网站 可以做淘宝客吗
  • 本地wordpress环境搭建seo快速排名软件案例
  • 景德镇做网站哪家好如何做好营销型网站建设
  • 做网站设计怎么样室内设计奖项有哪些
  • 江门市专业做网站公司做网站时怎么更改区域内的图片
  • 做一个电子商城网站建设方案淮安企业网站制作
  • 茂名网站制作维护wordpress搜索增加条件
  • 河北邢台是不是很穷网站站内优化案例
  • 网站建设公司排网站地图做计划任务
  • 如何建购物网站东莞中堂网站建设
  • 做网站全屏尺寸是多少钱wordpress侧边栏位置
  • 麻章手机网站建设销客多微分销系统
  • 宁波做网站seowordpress企业主题 视频教程
  • 企业网站的域名是该企业的推推蛙网站诊断
  • 网站开发的网站广东省建设工程金匠奖公布网站
  • 网站优化推广排名调试网站解析域名影响
  • 网站备案 机构需要什么手续网站建设需要学什么证
  • 网站源码在线查看银川公司网站建设
  • 网站搭建中单页面帮助做ppt的网站
  • 自己做网站需要收费吗python基础教程电子版
  • 检查网站的死链接网页游戏排行榜人气
  • 怎么做外国网站流量网页设计实训总结报告三千字
  • 企业网站程序下载用动态和静态设计一个网站
  • 重庆建企业网站mysql 学习网站