ApplicationContextInitializer、PropertySourceLocator自定义bootstrap属性源

PropertySourceLocator自定义bootstrap属性源

PropertySourceLocator 接口是 Spring Boot 中用于定位属性源的接口,它的作用是在应用程序启动时加载配置文件,并将其转换为 PropertySource 对象,以便应用程序可以使用这些属性。

实现 PropertySourceLocator 接口,需要重写 locate 方法。该方法的作用是根据给定的环境和属性源名称查找属性源,并返回一个 PropertySource 对象。

示例代码:

import org.springframework.boot.env.PropertySourceLocator;
import org.springframework.core.env.*;

public class MyPropertySourceLocator implements PropertySourceLocator {

   @Override
   public PropertySource<?> locate(Environment environment) {
       Map<String,Object> params = new HashMap<>();
       params.put("name","Lucifer");
       params.put("age","25");
       MapPropertySource mapPropertySource = new MapPropertySource("myPropertySource",params);
       return mapPropertySource;
   }
}

spring.factories中配置自定义配置类

#在Spring Cloud应用程序中,BootstrapConfiguration是一个特殊的配置类,它用于在应用程序启动时加载配置信息。它的作用是在应用程序启动之前,提供一种机制来加载一些基础的配置信息,例如从配置中心加载配置信息、设置环境变量等。这些配置信息可以被应用程序中的其他组件所使用,例如Spring的配置文件、Java代码等。

#在Spring Cloud中,BootstrapConfiguration是通过spring.factories文件来自动加载的。当Spring Boot应用程序启动时,它会扫描classpath下的所有spring.factories文件,并加载其中的所有配置类。在这些配置类中,如果存在org.springframework.cloud.bootstrap.BootstrapConfiguration的实现类,那么它们将会被自动加载并执行。

org.springframework.cloud.bootstrap.BootstrapConfiguration=com.lucifer.utils.MyPropertySourceLocator 


ApplicationContextInitializer自定义bootstrap属性源

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.HashMap;
import java.util.Map;

public class CustomBootstrapPropertySourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        Map<String, Object> customProperties = new HashMap<>();
        customProperties.put("custom.property", "custom value");
        propertySources.addFirst(new MapPropertySource("customBootstrapProperties", customProperties));
    }
}

建了一个名为CustomBootstrapPropertySourceInitializer的类,实现了ApplicationContextInitializer接口。initialize方法中,我们获取了应用程序上下文的可配置环境,并获取了可变属性源。然后,创建了一个包含自定义属性的Map,并将其添加到属性源的第一个位置。这样,就可以在应用程序中使用自定义属性了。

spring.factories中配置

org.springframework.cloud.bootstrap.BootstrapConfiguration=com.example.CustomBootstrapPropertySourceInitializer

参考文章:https://www.jianshu.com/p/377a9b4c3c56

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Bootstrap自定义排序,您可以使用以下步骤: 1. 首先,您需要使用 JavaScript 将您的数据排序。 2. 然后,使用 jQuery 将您的已排序数据添加到 Bootstrap 的表格中。 3. 最后,您需要重新绘制 Bootstrap 表格以使用新的排序数据。 下面是一个简单的示例,演示如何使用 JavaScript 和 jQuery 自定义排序 Bootstrap 表格: HTML 代码: ```html <table id="myTable" class="table"> <thead> <tr> <th>名称</th> <th>价格</th> <th>数量</th> </tr> </thead> <tbody> <tr> <td>商品 A</td> <td>100</td> <td>5</td> </tr> <tr> <td>商品 B</td> <td>50</td> <td>10</td> </tr> <tr> <td>商品 C</td> <td>200</td> <td>2</td> </tr> </tbody> </table> ``` JavaScript 代码: ```javascript // 自定义排序函数 function sortTable(table, column, asc) { var tbody = table.find('tbody'); var rows = tbody.find('tr').toArray(); rows.sort(function(a, b) { var aVal = $(a).children('td').eq(column).text(); var bVal = $(b).children('td').eq(column).text(); if (asc) { return (aVal > bVal) ? 1 : -1; } else { return (aVal < bVal) ? 1 : -1; } }); $.each(rows, function(index, row) { tbody.append(row); }); } // 切换排序按钮点击事件 $('#myTable th').click(function() { var table = $(this).closest('table'); var column = $(this).index(); var asc = $(this).hasClass('asc'); table.find('th').removeClass('asc desc'); if (asc) { $(this).addClass('desc'); } else { $(this).addClass('asc'); } sortTable(table, column, asc); }); // 初始排序 sortTable($('#myTable'), 0, true); ``` 在这个示例中,我们将表头单元格的点击事件绑定到 `sortTable()` 函数,该函数将使用 `asc` 参数指定升序或降序排序。在单元格上添加 `.asc` 或 `.desc` 类将更改箭头的方向。`sortTable()` 函数将使用 jQuery 的 `text()` 方法获取单元格的值,然后将行按升序或降序排序,并使用 `append()` 方法将它们添加回 Bootstrap 表格中。 您可以根据需要自定义此示例,例如使用不同的排序算法、添加更多的列、使用 AJAX 从服务器检索数据等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值