简体   繁体   中英

How to access application.yml properties in Spring 1.5.9

I'm spinning my wheels on this.

Here is a simple Spring Boot app. I'm trying to use a yaml properties file, but I can't seem to get it to find the properties file or let me access the values in it. I've tried many variations from advice I've searched for here and elsewhere. Nothing is working.

(For reasons I can't go into, I have to use an older version of Spring. (1.5.9))

In IntelliJ I have the Lombok plugin installed, annotations enabled and the language is set to Java 1.8.

Here is the error I'm getting currently:

[ERROR] demo/src/main/java/com/example/demo/Foo.java:[9,10] cannot find symbol
[ERROR] symbol:   method value()
[ERROR] location: @interface lombok.Value
[ERROR] demo/src/main/java/com/example/demo/Foo.java:[9,3] annotation type not applicable to this kind of declaration
[ERROR] demo/src/main/java/com/example/demo/Foo.java:[13,5] cannot find symbol
[ERROR] symbol:   variable log
[ERROR] location: class com.example.demo.Foo

Here is the folder structure:

在此处输入图片说明

Here is the application.yml file:

project:
  thing:
    path:  resources/foo_files

Here is the main class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {

    Foo foo = new Foo();

    SpringApplication.run(DemoApplication.class, args);
  }
}

Here is class Foo:

package com.example.demo;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class Foo {

  @Value("${project.thing.path}")
  private String projectThingPath;

  public Foo() {
    log.info("path is: " + projectThingPath);
  }
}

Here is the 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

For getting value from application.yml you should be using @Value from the package org.springframework.beans.factory.annotation.Value but you are using lombok's @Value

package com.example.demo;

import org.springframework.beans.factory.annotation.Value; // Changed
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component // EDIT : Added
class Foo {

  @Value("${project.thing.path}")
  private String projectThingPath;

  public Foo() {
    log.info("path is: " + projectThingPath);
  }
}

EDIT

So another important point is that, a class should be annotated with stereotype annotation ( To tell Spring to configure the data).

You have compile error because you are using @Slf4j (lombok annotation) but you have not Slf4j lib in your dependencies

In this case you can use @Log or add Slf4j to your dependencies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM