java Spring boot 无法解析占位符 application.yml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46166941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot Could not resolve placeholder application.yml
提问by Lexo Alonzo
i'm new in spring and i have a trouble with it.
When i run my application with IntelliJ Idea everything works perfect, but when i compile it to a .jar application this give me this error java.lang.IllegalArgumentException: Could not resolve placeholder 'jwt.secret' in value "${jwt.secret}"
我是春天新来的,我遇到了麻烦。当我使用 IntelliJ Idea 运行我的应用程序时,一切正常,但是当我将它编译为 .jar 应用程序时,这给了我这个错误java.lang.IllegalArgumentException: Could not resolve placeholder 'jwt.secret' in value "${jwt.secret}"
This is my application.yml
located at src/main/resources
这是我application.yml
位于src/main/resources
#Config Application
jwt:
header: Authorization
secret: mySecret
expiration: 604800
route:
authentication:
path: auth
refresh: refresh
token: check
When i change my to this it works perfect.
当我改变我的这个它工作完美。
// @Value("${jwt.secret}")
@Value("mySecret")
private String secret;
What i'm doing wrong?
我在做什么错?
Note: i'm using MAVEN
注意:我正在使用 MAVEN
This is my pom.xml
这是我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.test</groupId>
<artifactId>test-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-app</name>
<description>Test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<jjwt.version>0.7.0</jjwt.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
采纳答案by Lexo Alonzo
My bad i was building with Intellij Idea. Build using this command:
我的坏处是用 Intellij Idea 构建。使用以下命令构建:
./mvnw clean package
回答by wwj
@Autowired private Environment env;
@Autowired 私有环境环境;
env.getProperty("jwt.secret")
env.getProperty("jwt.secret")
you can try!
你可以试试!
回答by Henry Kurz
You have a tiny mistake there. Look at your application.yml:
你有一个小错误。看看你的 application.yml:
jwt:
header: Authorization
secret: mySecret
The property name is "secret" and not "mySecret". You accidentally bind to the value instead of the property name. Simply replace:
属性名称是“secret”而不是“mySecret”。您不小心绑定到值而不是属性名称。简单地替换:
@Value("mySecret")
by this:
这样:
@Value("secret")
Your error message however indicates that you used the commented out "jws.secret". Please keep in mind that if you use a prefix (e.g. "jws") then you can refer to that property by "secret" only.
但是,您的错误消息表明您使用了注释掉的“jws.secret”。请记住,如果您使用前缀(例如“jws”),那么您只能通过“秘密”来引用该属性。