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


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

java spring @PropertySource注解使用

 

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

©Copyright 蕃薯耀 2020-12-02

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

 

一、在application.properties文件自定義變量:jjwt.key

jjwt.key=aXNsZWVfaGFoYQ==

 

二、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;
    }
    
}

 

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

1、聲明自定義配置類

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;
    }
}

 

2、使用@Autowired注解注入靜態變量

@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();
    }
}

 

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

1、my.properties配置文件:

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

 

2、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
                + "]";
    }
}

 

五、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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM