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 注入静态变量)
@Componentpublic classJwtUtils {//声明静态变量
private staticString secretKey;/*** 静态变量注入
* 从配置文件读取jjwt.key属性
* 注入key,set方法不能是static
*@paramsecretKey*/@Value("${jjwt.key}")public voidsetSecretKey(String secretKey) {
JwtUtils.secretKey=secretKey;
}
}
三、springboot @ConfigurationProperties注解使用,并注入到静态变量
1、声明自定义配置类
importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix= "jjwt", ignoreUnknownFields = true)public classJjwtProperties {privateString key;publicString getKey() {returnkey;
}public voidsetKey(String key) {this.key =key;
}
}
2、使用@Autowired注解注入静态变量
@Componentpublic classJwtUtils {//声明静态变量
private static String aa;//测试静态变量注入
/*** 静态实体变量注入
* jjwtProperties需要配置:@ConfigurationProperties(prefix = "jjwt", ignoreUnknownFields = true)
*@paramjjwtProperties*/@Autowiredpublic voidsetSecretKey(JjwtProperties jjwtProperties) {
JwtUtils.aa=jjwtProperties.getKey();
}
}
四、springboot @PropertySource读取自定义配置文件
1、my.properties配置文件:
my.name=哈哈
my.age=25my.clazz=语言, 文学, 科学
my.map.aa=这是
my.map.bb=一个
my.map.cc=map对象
2、my.properties对应的配置类MyProperties.class
importjava.util.Arrays;importjava.util.Map;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value= {"classpath:my.properties"}, encoding = "UTF-8")
@ConfigurationProperties(ignoreUnknownFields= true, prefix = "my")public classMyProperties {privateString name;privateInteger age;privateString[] clazz;private Mapmap;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicInteger getAge() {returnage;
}public voidsetAge(Integer age) {this.age =age;
}publicString[] getClazz() {returnclazz;
}public voidsetClazz(String[] clazz) {this.clazz =clazz;
}public MapgetMap() {returnmap;
}public void setMap(Mapmap) {this.map =map;
}
@OverridepublicString toString() {return "MyProperties [name=" + name + ", age=" + age + ", clazz=" + Arrays.toString(clazz) + ", map=" +map+ "]";
}
}
五、Controller测试
importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importcom.lqy.study.bean.Result;importcom.lqy.study.biz.jjwt.JjwtProperties;importcom.lqy.study.biz.jjwt.JwtUtils;importcom.lqy.study.biz.jjwt.MyProperties;importcn.hutool.core.bean.BeanUtil;
@RestController
@RequestMapping("/jwt")public classJwtController {
@AutowiredprivateJjwtProperties jjwtProperties;
@AutowiredprivateMyProperties myProperties;
@RequestMapping("/p")public Result properties() throwsParseException {returnResult.ok(jjwtProperties);
}
@RequestMapping("/my")public Result my() throwsParseException {return Result.ok(BeanUtil.copyProperties(myProperties, MyProperties.class));//这里不能直接输出myProperties
}
}
================================
©Copyright 蕃薯耀 2020-12-02
https://www.cnblogs.com/fanshuyao/