【问题标题】:Spring boot Could not resolve placeholder application.ymlSpring boot 无法解析占位符 application.yml
【发布时间】:2018-02-20 08:36:47
【问题描述】:

我是春天的新手,我遇到了麻烦。 当我使用 IntelliJ Idea 运行我的应用程序时,一切正常,但是当我将其编译为 .jar 应用程序时,这给了我这个错误java.lang.IllegalArgumentException: Could not resolve placeholder 'jwt.secret' in value "${jwt.secret}"

这是我的application.yml,位于src/main/resources

#Config Application
jwt:
  header: Authorization
  secret: mySecret
  expiration: 604800
  route:
    authentication:
      path: auth
      refresh: refresh
      token: check

当我将我的更改为这个时,它会完美运行。

//    @Value("${jwt.secret}")
    @Value("mySecret")
    private String secret;

我做错了什么?

注意:我使用的是 MAVEN

这是我的 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>

【问题讨论】:

  • 你是怎么编译的?你用的是maven吗?这是一个 Spring Boot 应用程序吗?
  • 试试这个,@ConfigurationProperties("jwt") public class CustomCacheConfiguration { @Value("${jwt.secret}") private String secret; ... }
  • 我正在使用 maven
  • @AndyAlonzo 发布您的pom.xml
  • pom.xml 已发布:D

标签: java maven spring-boot intellij-idea yaml


【解决方案1】:

我是用 Intellij Idea 构建的。 使用此命令构建:

./mvnw clean package

【讨论】:

    【解决方案2】:
    @Autowired
    private Environment env;
    
    env.getProperty("jwt.secret")
    

    你可以试试!

    【讨论】:

    • 用application.properties试试?
    【解决方案3】:

    你有一个小错误。 查看您的 application.yml:

     jwt:
      header: Authorization
      secret: mySecret
    

    属性名称是“secret”而不是“mySecret”。您不小心绑定到值而不是属性名称。只需替换:

    @Value("mySecret")
    

    通过这个:

    @Value("secret")
    

    但是,您的错误消息表明您使用了注释掉的“jws.secret”。请记住,如果您使用前缀(例如“jws”),那么您只能通过“secret”来引用该属性。

    【讨论】:

    • 这是高度误导,因为您使用的是常量而不是表达式。例如。缺少 ${...}。
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2016-07-24
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多