
本文旨在提供一种解决方案,允许 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;
}说明:
创建 DAO 接口:
接下来,我们需要创建一个 DAO(Data Access Object)接口,用于访问数据库中的配置信息。 我们使用 Spring Data JPA 来简化数据库访问。
import org.springframework.data.jpa.repository.JpaRepository;
public interface DynamicConfigDao extends JpaRepository<DynamicConfig,Integer> {
DynamicConfig findByKey(String key);
}说明:
创建自定义 PropertySource:
现在,我们需要创建一个自定义的 PropertySource,该 PropertySource 从数据库读取配置信息,并将其暴露给 Spring 的 Environment。
import org.springframework.core.env.EnumerablePropertySource;
public class DynamicConfigPropertySource extends EnumerablePropertySource<DynamicConfigDao> {
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();
}
}说明:
注册自定义 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;
@SpringBootApplication
public 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));
}
}
}说明:
完成以上步骤后,我们就可以像使用 application.properties 文件一样,使用 @Value 注解或 Environment 对象来访问数据库中的配置信息。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Value("${my.dynamic.property}")
private String myDynamicProperty;
public String getMyDynamicProperty() {
return myDynamicProperty;
}
}说明:
本文介绍了一种基于数据库动态配置 Spring Boot 应用属性的解决方案。 通过自定义 PropertySource,我们可以将数据库中的配置项集成到 Spring 的属性管理体系中,实现配置的动态更新和管理。 这种方案可以提高应用的灵活性和可维护性,避免每次修改配置都需要重启服务器。
以上就是基于数据库动态配置 Spring Boot 应用属性的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号