一、Spring boot自定义配置实现自动提示
@ConfigurationProperties
的作用: 让JavaBean中属性值要和配置文件进行映射
@Getter
@Setter
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {
/**
* JWT加解密使用的密钥
*/
private String secret;
/**
* JWT的过期限时间
*/
private String expiration;
/**
* JWT负载中拿到开头
*/
private String tokenHead;
/**
* JWT存储的请求头
*/
private String tokenHeader;
}
二、在启动类中加入@EnableConfigurationProperties 注解
@EnableConfigurationProperties 注解的作用:让使用了 @ConfigurationProperties 注解的类生效。
@EnableConfigurationProperties({JwtProperties.class})
@SpringBootApplication
public class SeadogAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SeadogAdminApplication.class, args);
}
}
三、idea中会提示SpringBoot Configuration Annotation Processor not configured的错误
四、解决方法
您在使用@ConfigurationProperties注解时可以使用spring-boot-configuration-processor jar 轻松地从带有注释的项目中生成自己的配置元数据文件。该jar包含一个Java注释处理器,在您的项目被编译时会被调用。要使用处理器,请包含对的依赖spring-boot-configuration-processor。
1.添加spring-boot-configuration-processor的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
重新buil了项目,你会发现问题还没有解决,idea还是提示:SpringBoot Configuration Annotation Processor not configured的错误。
2.在maven-compiler-plugin内的annotationProcessorPaths中添加相应path
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${springboot.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
3.maven-clean 重新build一下项目,会在target->classes->META-INF目录下生成配置元数据文件:spring-configuration-metadata.json
打开spring-configuration-metadata.json文件
{
"groups": [
{
"name": "jwt",
"type": "com.seadog.common.security.config.JwtProperties",
"sourceType": "com.seadog.common.security.config.JwtProperties"
}
],
"properties": [
{
"name": "jwt.expiration",
"type": "java.lang.String",
"description": "JWT的过期限时间",
"sourceType": "com.seadog.common.security.config.JwtProperties"
},
{
"name": "jwt.secret",
"type": "java.lang.String",
"description": "JWT加解密使用的密钥",
"sourceType": "com.seadog.common.security.config.JwtProperties"
},
{
"name": "jwt.token-head",
"type": "java.lang.String",
"description": "JWT负载中拿到开头",
"sourceType": "com.seadog.common.security.config.JwtProperties"
},
{
"name": "jwt.token-header",
"type": "java.lang.String",
"description": "JWT存储的请求头",
"sourceType": "com.seadog.common.security.config.JwtProperties"
}
],
"hints": []
}
4.在application.yml中测试
标签:JwtProperties,String,Spring,configured,boot,JWT,Boot,spring,configuration From: https://www.cnblogs.com/javaxubo/p/17352155.html