1.@ConfigurationProperties注解
总体概述:读取相关的配置文件获取指定的参数值
两种使用方法:
1)搭配@Bean使用绑定第三方属性,@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。如实现自动任务消息推送
@Configuration
public class AutoTaskConfig {
//注入构件到容器中,创建这个类的实体对象
@Bean("构件ID")
public AutoTaskService sendMsg(){
return new AutoTaskService();
}
}
2)搭配@Component注解使用获取,@Component注解表明一个类会作为组件类,然后spring会为该类创建bean。把配置信息封装为JavaBean获取相应的属性值,如果不用@component修饰。则在容器无法获取,如果只使用@ConfigurationProperties需要结合@EnableConfigurationProperties(PropertisInject.class)将其注册到spring容器中。
@Data
@Component
@Configuration
@ComponentScan(basePackages = "com.service")
@EntityScan(basePackages = "com.entity")
@ConfigurationProperties(prefix = "configurations.authentication")
public class YamlConfig {
private Map<String, Object> SingleLogin;
private Map<String, Object> DEENcrypt;
private String timeOut;
private String tenantId;
}
2.请求获取头部
三种方式:
1)request.getHeader(“Accept-Encoding”):获取单个请求头name的value值
2)request.getHeaders(“Accept-Encoding”):获取多个同名请求头对应的一组value值,返回枚举类型数据。
3)request.getHeaderNames():获取请求头的所有name值,返回的数据也是一个枚举类型的数据,将枚举中的元素依次遍历出来,根据name获取对应的value值,即可得到Http请求头的所有信息。
请求头遍历方式为:
//header
Map<String, String> headerMap = new HashMap<>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
headerMap.put(key, value);
}