Java ConfigurableEnvironment

Java ConfigurableEnvironment 是 Spring 框架中的一个接口,其作用是允许应用程序在运行时获取和设置配置信息。ConfigurableEnvironment 实现了 Environment 接口,并添加了一些设置方法,例如可以在运行时添加新的 property、修改 active profiles 等。ConfigurableEnvironment 还提供了方便的方法来获取配置属性和 profile 信息。

以下是一个使用 ConfigurableEnvironment 获取配置信息的示例:

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

@Autowired
private ConfigurableEnvironment env;

public void printProperties() {
    MutablePropertySources propertySources = env.getPropertySources();
    for (PropertySource<?> propertySource : propertySources) {
        System.out.println(propertySource.getName());
        System.out.println(propertySource.getSource().getClass());
        for (String propName : propertySource.getPropertyNames()) {
            System.out.println(propName + "=" + propertySource.getProperty(propName));
        }
    }
}

在上述示例中,我们使用了 @Autowired 注解来注入 ConfigurableEnvironment 实例 env。接着,我们调用其 getPropertySources() 方法来获取一个 MutablePropertySources 实例,它包含了所有的配置信息。我们使用 for 循环遍历这些 property source,并输出它们的名称、类型以及相应的配置属性。

ConfigurableEnvironment 还提供了一些其他的方法,例如可通过 setActiveProfiles() 方法来设置当前的 active profiles,以及通过 getProperty() 方法来获取指定的属性值。此外,ConfigurableEnvironment 还可以与 Consul 等配置中心集成,方便实现动态配置。

总的来说,ConfigurableEnvironment 是 Spring 框架中用来获取和配置属性的一个关键组件,它提供了丰富的 API 用来访问和修改应用程序的配置信息。应用程序可以通过 ConfigurableEnvironment 来实现动态配置、多环境等功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java开发中,我们可以使用数据库来保存不同环境的配置信息,然后在Spring Boot应用启动时从数据库中加载配置信息。下面是实现步骤: 1. 创建一个数据库表,用于保存不同环境的配置信息,例如: ```sql CREATE TABLE `config` ( `id` int(11) NOT NULL AUTO_INCREMENT, `env` varchar(50) NOT NULL COMMENT '环境名称', `property_key` varchar(50) NOT NULL COMMENT '配置项的键', `property_value` varchar(200) NOT NULL COMMENT '配置项的值', PRIMARY KEY (`id`), KEY `idx_env_key` (`env`,`property_key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='配置信息表'; ``` 2. 在Spring Boot应用的配置文件中,配置数据源的连接信息和JPA相关信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true ``` 3. 创建一个实体类,用于映射配置信息表中的数据,例如: ```java package com.example.demo.entity; import lombok.Data; import javax.persistence.*; @Data @Entity @Table(name = "config") public class Config { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String env; @Column(name = "property_key") private String key; @Column(name = "property_value") private String value; } ``` 4. 创建一个Repository接口,用于访问数据库中的配置信息,例如: ```java package com.example.demo.repository; import com.example.demo.entity.Config; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface ConfigRepository extends JpaRepository<Config, Integer> { List<Config> findByEnv(String env); } ``` 5. 创建一个Service类,用于从数据库中加载配置信息,例如: ```java package com.example.demo.service; import com.example.demo.entity.Config; import com.example.demo.repository.ConfigRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class ConfigService { @Autowired private ConfigRepository configRepository; public Map<String, String> getConfig(String env) { List<Config> configList = configRepository.findByEnv(env); Map<String, String> configMap = new HashMap<>(); for (Config config : configList) { configMap.put(config.getKey(), config.getValue()); } return configMap; } } ``` 6. 在应用启动时,调用ConfigService类的getConfig方法,将获取到的配置信息保存到Spring Boot的Environment中,例如: ```java package com.example.demo; import com.example.demo.service.ConfigService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertiesPropertySource; import java.util.Map; import java.util.Properties; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args); ConfigurableEnvironment environment = applicationContext.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); ConfigService configService = applicationContext.getBean(ConfigService.class); Map<String, String> configMap = configService.getConfig("dev"); Properties properties = new Properties(); properties.putAll(configMap); PropertiesPropertySource propertySource = new PropertiesPropertySource("config", properties); propertySources.addLast(propertySource); } } ``` 这样,当应用启动时,就会从数据库中加载配置信息,并将配置信息保存到Spring Boot的Environment中,我们就可以通过@ConfigurationProperties注解来注入这些配置信息了。 以上就是在Java开发中如何将不同环境的配置信息保存到数据库,并在Spring Boot应用启动时从数据库中加载配置信息的实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值