SpringBoot 静态属性注入

将如下的 application.yml 配置文件的属性值注入到配置类的静态变量中

spring:
    profiles:
        active: dev
system:
    account: 123456
    password: 123456
    appid: 1234567890

方式一 使用 @Value() 注解

通过set方法来赋值。属性是static修饰的,get方法也是static修饰的,但是set方法不能是static修饰,使用@Value()注解来修饰set方法

@Configuration
public class SystemApiConfig {

    /**账号*/
    private static String account;

    /**密码*/
    private static String password;

    /**平台三方系统分配的id*/
    private static String appid;

    @Value("${system.account}")
    public void setAccouont(String account){
        this.account = account;
    }

    @Value("${system.password}")
    public void setPassword(String password){
        this.password = password;
    }

    @Value("${system.appid}")
    public void setAppid(String appid){
        this.appid = appid;
    }

    public static String getAccount() {
        return account;
    }

    public static String getPassword() {
        return password;
    }

    public static String getAppid() {
        return appid;
    }
}

方式二 @ConfigurationProperties(prefix = ) 注解

只要把set方法设置为非静态,那么这个配置类的静态属性就能成功注入了

@Configuration
@ConfigurationProperties(prefix = "system")
public class SystemApiConfig {

    /**账号*/
    private static String account;

    /**密码*/
    private static String password;

    /**平台三方系统分配的id*/
    private static String appid;

    public static String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        SystemApiConfig.account = account;
    }

    public static String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        SystemApiConfig.password = password;
    }

    public static String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        SystemApiConfig.appid = appid;
    }
}

方式三 @PostConstruct 注解

@PostConstruct 用来修饰一个非静态的void方法。它会在服务器加载Servlet的时候运行,并且只运行一次

@Configuration
public class SystemApiConfig {

    public static String environment;

    /*从配置文件中获取值**/
    @Value("${spring.profiles.active}")
    public String active;

    @PostConstruct
    public void initializeEnvironment() {
        environment = this.active;
    }
    
    private static RedisTemplate<Object, Object> redisTemplates;

    /**从容器中注入*/
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @PostConstruct
    public void initializeRedisTemplate() {
        redisTemplates = this.redisTemplate;
    }
}

方式四 实现 InitializingBean 接口,重写 afterPropertiesSet() 方法

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SystemApiConfig implements InitializingBean {

    @Value("${system.account}")
    private String account;

    @Value("${system.password}")
    private String password;

    @Value("${system.appid}")
    private String appid;

    public static String ACCOUNT;
    
    public static String PASSWORD;
    
    public static String APPID;

    @Override
    public void afterPropertiesSet() throws Exception {
        ACCOUNT=account;
        PASSWORD=password;
        APPID=appid;
    }
}
posted @ 2022-05-15 21:44  伊文小哥  阅读(2579)  评论(0编辑  收藏  举报