springboot @Value 靜態變量注入,springboot @ConfigurationProperties註解使用

springboot @Value 靜態變量注入,springboot @ConfigurationProperties註解使用java

java spring @PropertySource註解使用web

 

================================spring

©Copyright 蕃薯耀 2020-12-02springboot

https://www.cnblogs.com/fanshuyao/app

 

1、在application.properties文件自定義變量:jjwt.keyide

jjwt.key=aXNsZWVfaGFoYQ==

 

2、springboot @Value靜態變量注入(@Value 注入靜態變量)測試

@Component
public class JwtUtils {
    
    //聲明靜態變量
    private static String secretKey;
    
    /**
     * 靜態變量注入
     * 從配置文件讀取jjwt.key屬性
     * 注入key,set方法不能是static
     * @param secretKey
     */
    @Value("${jjwt.key}")
    public void setSecretKey(String secretKey) {
        JwtUtils.secretKey = secretKey;
    }
    
}

 

3、springboot @ConfigurationProperties註解使用,並注入到靜態變量this

一、聲明自定義配置類spa

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "jjwt", ignoreUnknownFields = true)
public class JjwtProperties {

    private String key;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

 

二、使用@Autowired註解注入靜態變量code

@Component
public class JwtUtils {
    
    //聲明靜態變量
    private static String aa;//測試靜態變量注入

    /**
     * 靜態實體變量注入
     * jjwtProperties須要配置:@ConfigurationProperties(prefix = "jjwt", ignoreUnknownFields = true)
     * @param jjwtProperties
     */
    @Autowired
    public void setSecretKey(JjwtProperties jjwtProperties) {
        JwtUtils.aa = jjwtProperties.getKey();
    }
}

 

4、springboot @PropertySource讀取自定義配置文件

一、my.properties配置文件:

my.name=哈哈
my.age=25
my.clazz=語言, 文學, 科學
my.map.aa=這是
my.map.bb=一個
my.map.cc=map對象

 

二、my.properties對應的配置類MyProperties.class

import java.util.Arrays;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = {"classpath:my.properties"}, encoding = "UTF-8")
@ConfigurationProperties(ignoreUnknownFields = true, prefix = "my")
public class MyProperties {

    private String name;
    private Integer age;
    private String[] clazz;
    private Map<String, Object> map;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String[] getClazz() {
        return clazz;
    }
    public void setClazz(String[] clazz) {
        this.clazz = clazz;
    }
    
    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    
    @Override
    public String toString() {
        return "MyProperties [name=" + name + ", age=" + age + ", clazz=" + Arrays.toString(clazz) + ", map=" + map
                + "]";
    }
}

 

5、Controller測試

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lqy.study.bean.Result;
import com.lqy.study.biz.jjwt.JjwtProperties;
import com.lqy.study.biz.jjwt.JwtUtils;
import com.lqy.study.biz.jjwt.MyProperties;

import cn.hutool.core.bean.BeanUtil;

@RestController
@RequestMapping("/jwt")
public class JwtController {

    @Autowired
    private JjwtProperties jjwtProperties;
    
    @Autowired
    private MyProperties myProperties;
    
    
    @RequestMapping("/p")
    public Result properties() throws ParseException {
        return Result.ok(jjwtProperties);
    }
    
    
    @RequestMapping("/my")
    public Result my() throws ParseException {
        return Result.ok(BeanUtil.copyProperties(myProperties, MyProperties.class));//這裏不能直接輸出myProperties
    }
}

 

 

 

================================

©Copyright 蕃薯耀 2020-12-02

https://www.cnblogs.com/fanshuyao/

相關文章
相關標籤/搜索