简体   繁体   中英

External Spring boot properties not read

I use spring boot 2.2.2 and I have some external properties I want to read from properties file. When properties are part of the default application.properties all properties are read but when I move it to separate properties file it seems that they are not read.

I have the following class:

@Getter
@ConfigurationProperties  //I use @ConfigurationPropertiesScan with @SpringBootApplication
@PropertySource(value = "classpath:myprops.properties")
public class MyProperties {

    private final String prop1;
    private final Integer prop2;
    private final String prop3;

    @ConstructorBinding
    public MyProperties (String prop1, Integer prop2, String prop3) {
        this.prop1= prop1;
        this.prop2= prop2;
        this.prop3= prop3;
    }
}

so prop1, prop2 and prop3 get their values when they are in application.properties (and of course omitting @PropertySource annotation) but when I move it to myprops.properties under resources I see that prop1, prop2 and prop3 are null as written above

Try to create the class annotated with @Configuration like in the docs

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:myprops.properties")
public class MyConfiguration {
}

remove the @PropertySource from MyProperties class and place it in the MyConfiguration .

Update:

Steps to make it work:

1) Go to https://start.spring.io . Generate project with added: Spring Configuration Processor and Lombok (enabled annotation processing)

2) Add or check:

  • myprops.properties to the resources (next to the application.properties)
  • MyConfiguration and MyProperties next to the class with the main method
package com.example.demostackannotations;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:myprops.properties")
public class MyConfiguration {
}
package com.example.demostackannotations;

import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@Getter
@ConfigurationProperties
public class MyProperties {

    private final String prop1;
    private final Integer prop2;
    private final String prop3;

    @ConstructorBinding
    public MyProperties(String prop1,  Integer prop2,  String prop3) {
        this.prop1 = prop1;
        this.prop2 = prop2;
        this.prop3 = prop3;
    }
}
  • @ConfigurationPropertiesScan to the class with the main method
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoStackAnnotationsApplication {

    public static void main(String[] args) {
        ApplicationContext app = SpringApplication.run(DemoStackAnnotationsApplication.class, args);
        MyProperties props = app.getBean(MyProperties.class);
        System.out.println(props.getProp1());
        System.out.println(props.getProp2());
        System.out.println(props.getProp3());
    }
}
  • 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 https://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>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-stack-annotations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-stack-annotations</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

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