从数据库检索 Spring Boot 配置

mip*_*mip 8 java configuration spring properties spring-boot

谁能为我提供一些关于实现这一目标的最佳方法的指导。

我想扩展 Spring Boot外部化配置,以便我有一个可以从应用程序中的任何位置调用的方法。此方法将使用键检索属性值。此方法将首先询问数据库表,如果没有找到指定的键,则将退回到1中描述的 PropertySource 顺序。

所以我会有类似的服务:

@Service
public class ConfigurationService {

    private final ConfigurationRepository configurationRepository;

    @Autowired
    public ConfigurationService(ConfigurationRepository configurationRepository) {
        this.configurationRepository = configurationRepository;
    }

    public String getValue(String key) {
        Configuration configuration = configurationRepository.findOne(key);

        // Add something here to get the property from application.properties if the key does not exist in the db

        return configuration == null ? null : configuration.getValue();
    }

}
Run Code Online (Sandbox Code Playgroud)

我可以使用如下:

foo = configuration.getValue("my.property");
Run Code Online (Sandbox Code Playgroud)

有更好的方法来解决这个问题吗?我是否缺少可以利用的 Spring Boot 功能?

我希望能够在应用程序运行时更改属性的值并获取这些新值。

Fed*_*zza 10

我使用了 EnvironmentPostProcessor spring 功能来执行此操作。

您需要创建一个像这样的类:

public class ReadDbPropertiesPostProcessor implements EnvironmentPostProcessor {
    /**
     * Name of the custom property source added by this post processor class
     */
    private static final String PROPERTY_SOURCE_NAME = "databaseProperties";
    
    /**
     * Adds Spring Environment custom logic. This custom logic fetch properties from database and setting highest precedence
     */
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Map<String, Object> propertySource = new HashMap<>();

        try {
            // Build manually datasource to ServiceConfig
            DataSource ds = DataSourceBuilder
                    .create()
                    .username(USERNAME) // replace with your config
                    .password(PASSWORD) // replace with your config
                    .url(DATASOURCE-URL)// replace with your config
                    .driverClassName(DRIVER) // replace with your config
                    .build();

            // Fetch all properties
            PreparedStatement preparedStatement = ds.getConnection().prepareStatement("SELECT name, value FROM propertyConfig WHERE service = ?");
            preparedStatement.setString(1, APP_NAME);
            
            ResultSet rs = preparedStatement.executeQuery();

            // Populate all properties into the property source
            while (rs.next()) {
                String propName = rs.getString("name");
                propertySource.put(propName, rs.getString("value"));
            }
            
            // Create a custom property source with the highest precedence and add it to Spring Environment 
            environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
        
        } catch (Exception e) {
            throw new RuntimeException("Error fetching properties from db");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于您需要在 Spring 的早期阶段运行此类,因此您需要创建文件spring.factories并注册您的环境后处理器。该文件需要位于此处:

src/main/resources/META-INF/spring.factories
Run Code Online (Sandbox Code Playgroud)

在内容中,您需要将类设置为 spring 属性:

# Environment Post Processor
org.springframework.boot.env.EnvironmentPostProcessor=com.your.package.ReadDbPropertiesPostProcessor
Run Code Online (Sandbox Code Playgroud)