
本文旨在提供一种解决方案,允许 Spring Boot 应用从数据库动态加载和配置属性,从而避免每次修改配置都需要重启服务器。通过自定义 PropertySource,我们可以将数据库中的配置项集成到 Spring 的属性管理体系中,实现配置的动态更新和管理。
实现原理
核心思想是创建一个自定义的 PropertySource,该 PropertySource 从数据库读取配置信息,并将其暴露给 Spring 的 Environment。 Spring 在启动时会加载所有的 PropertySource,并将其中的属性合并到一个全局的属性集合中。 这样,我们就可以像使用 application.properties 文件一样,使用 @Value 注解或 Environment 对象来访问数据库中的配置信息。
具体步骤
创建数据库实体类:
首先,我们需要创建一个实体类来映射数据库中的配置表。该表至少应包含 key 和 value 两列,分别用于存储配置项的名称和值。
import lombok.Getter;import lombok.Setter;import javax.persistence.*;@Setter@Getter@Entity@Table(name = "t_dynamic_config")public class DynamicConfig { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String key; private String value;}
说明:
@Entity 和 @Table 注解用于将该类映射到数据库中的 t_dynamic_config 表。@Id 和 @GeneratedValue 注解用于指定主键和主键生成策略。key 和 value 字段分别用于存储配置项的名称和值。@Getter 和 @Setter 注解来自 Lombok 库,用于自动生成 getter 和 setter 方法,简化代码。
创建 DAO 接口:
接下来,我们需要创建一个 DAO(Data Access Object)接口,用于访问数据库中的配置信息。 我们使用 Spring Data JPA 来简化数据库访问。
import org.springframework.data.jpa.repository.JpaRepository;public interface DynamicConfigDao extends JpaRepository { DynamicConfig findByKey(String key);}
说明:
JpaRepository 接口提供了常用的数据库操作方法,如 findAll()、findById()、save() 等。findByKey() 方法用于根据配置项的名称查找配置信息。 Spring Data JPA 会自动根据方法名生成 SQL 查询语句。
创建自定义 PropertySource:
AppMall应用商店
AI应用商店,提供即时交付、按需付费的人工智能应用服务
56 查看详情
现在,我们需要创建一个自定义的 PropertySource,该 PropertySource 从数据库读取配置信息,并将其暴露给 Spring 的 Environment。
import org.springframework.core.env.EnumerablePropertySource;public class DynamicConfigPropertySource extends EnumerablePropertySource { public DynamicConfigPropertySource(String name, DynamicConfigDao source) { super(name, source); } @Override public String[] getPropertyNames() { return getSource().findAll().stream().map(DynamicConfig::getKey).toArray(String[]::new); } @Override public Object getProperty(String name) { return getSource().findByKey(name).getValue(); }}
说明:
EnumerablePropertySource 是 Spring 提供的一个抽象类,用于实现可枚举的 PropertySource。getPropertyNames() 方法返回所有配置项的名称。getProperty() 方法根据配置项的名称返回配置值。
注册自定义 PropertySource:
最后,我们需要将自定义的 PropertySource 注册到 Spring 的 Environment 中。 我们可以在 Spring Boot 应用的启动类中注册 PropertySource。
import org.springframework.beans.factory.SmartInitializingSingleton;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Component;import org.springframework.core.env.ConfigurableEnvironment;@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Component static class ConfigDynamicConfigPropertySource implements SmartInitializingSingleton { @Autowired private ConfigurableEnvironment environment; @Autowired private DynamicConfigDao dynamicConfigDao; @Override public void afterSingletonsInstantiated() { environment.getPropertySources().addLast(new DynamicConfigPropertySource("db_source",dynamicConfigDao)); } }}
说明:
SmartInitializingSingleton 接口用于在所有单例 bean 初始化完成后执行回调方法。afterSingletonsInstantiated() 方法在所有单例 bean 初始化完成后被调用。environment.getPropertySources().addLast() 方法将自定义的 PropertySource 添加到 Environment 的属性源列表中。
使用方式
完成以上步骤后,我们就可以像使用 application.properties 文件一样,使用 @Value 注解或 Environment 对象来访问数据库中的配置信息。
import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class MyComponent { @Value("${my.dynamic.property}") private String myDynamicProperty; public String getMyDynamicProperty() { return myDynamicProperty; }}
说明:
@Value(“${my.dynamic.property}”) 注解用于将 my.dynamic.property 配置项的值注入到 myDynamicProperty 字段中。如果数据库中不存在 my.dynamic.property 配置项,则 myDynamicProperty 字段的值为 null。
注意事项
确保数据库连接配置正确。确保数据库表结构与实体类定义一致。PropertySource 的名称应具有唯一性,避免与其他 PropertySource 冲突。在生产环境中,建议使用缓存来提高性能,避免频繁访问数据库。如果配置项的值发生变化,需要手动刷新 Environment,才能使新的配置生效。 可以通过 Spring 的 ContextRefresher 来实现配置的动态刷新。
总结
本文介绍了一种基于数据库动态配置 Spring Boot 应用属性的解决方案。 通过自定义 PropertySource,我们可以将数据库中的配置项集成到 Spring 的属性管理体系中,实现配置的动态更新和管理。 这种方案可以提高应用的灵活性和可维护性,避免每次修改配置都需要重启服务器。
以上就是基于数据库动态配置 Spring Boot 应用属性的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/222709.html
微信扫一扫
支付宝扫一扫