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/