springboot2.x 中 @Configuration类中,无法使用@Value注入配置

springboot2.x 中 @Configuration类中,无法使用@Value注入配置,获取的一直为null,此时需要通过Environment来获取,但是配置类需要实现EnvironmentAware接口。

/**
 * 实现EnvironmentAware接口的目的是,
 * 在@Configuration中无法通过@Value注解注入配置,
 * 所以通过Environment获取配置,但是需要实现EnvironmentAware接口
 */
@Configuration
public class ShiroConfig implements EnvironmentAware {

    private final String CACHE_KEY = "shiro:cache:";
    private final String SESSION_KEY = "shiro:session:";
    private final int EXPIRE = 1800;

    private Environment environment;
    private String host;
    private int port;
    private int timeout;
    private String password;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    /**
     * 初始化redis信息,通过environment获取配置信息,
     * 在@configuration中,@value无效
     */
    private void initRedis() {
        Optional<String> hostOpt = Optional.ofNullable(environment.getProperty("spring.redis.host"));
        host = hostOpt.orElse("localhost");
        Optional<String> portOpt = Optional.ofNullable(environment.getProperty("spring.redis.port"));
        port = Integer.valueOf(portOpt.orElse("6379"));
        Optional<String> timeoutOpt = Optional.ofNullable(environment.getProperty("spring.redis.timeout"));
        timeout = Integer.valueOf(timeoutOpt.orElse("1000"));
        Optional<String> passwordOpt = Optional.ofNullable(environment.getProperty("spring.redis.password"));
        password = passwordOpt.orElse("");
    }

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Spring Boot 的 `@Configuration` 类动态切换某一个值,可以通过注入 `Environment` 对象来实现。`Environment` 可以获取应用程序的所有属性,包括配置文件的属性和系统属性等。 假设你要切换的值是一个字符串类型的属性,可以通过以下步骤实现: 1. 在 `@Configuration` 类注入 `Environment` 对象。 ```java @Configuration public class AppConfig { @Autowired private Environment env; // ... } ``` 2. 在 `@Bean` 方法使用 `env.getProperty()` 方法获取属性值,并根据需要进行修改。 ```java @Bean public SomeBean someBean() { String value = env.getProperty("some.property.name"); // 根据需要修改 value 的值 return new SomeBean(value); } ``` 3. 如果需要在运行时动态修改属性值,可以使用 `MutablePropertySources` 和 `PropertySource` 对象。 ```java @Configuration public class AppConfig { @Autowired private ConfigurableEnvironment env; public void updatePropertyValue(String propertyName, String newValue) { MutablePropertySources sources = env.getPropertySources(); if (sources.contains("applicationProperties")) { PropertySource<?> propertySource = sources.get("applicationProperties"); if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource; String[] propertyNames = enumerablePropertySource.getPropertyNames(); for (String name : propertyNames) { if (propertyName.equals(name)) { env.getPropertySources().addFirst(new PropertySource<Object>("newProperties") { @Override public Object getProperty(String name) { return propertyName.equals(name) ? newValue : null; } }); return; } } } } } // ... } ``` 在上述代码,`updatePropertyValue()` 方法的 `propertyName` 参数指定要修改的属性名称,`newValue` 参数指定新的属性值。方法首先获取 `MutablePropertySources` 对象,然后查找名为 `applicationProperties` 的 `PropertySource` 对象。如果找到该对象并且是 `EnumerablePropertySource` 类型的,则遍历所有属性名,找到要修改的属性名后,使用 `env.getPropertySources().addFirst()` 方法添加一个新的 `PropertySource` 对象,覆盖原有的属性值。 以上是一种实现方式,具体实现可能会根据应用程序的需求而有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值