Home > Net >  Getting null value while fetching properties from application.properties file in Springboot
Getting null value while fetching properties from application.properties file in Springboot

Time:10-12

Objective : reading property value from application.properties file in my Java class .

Current behaviour : getting null values

Expected behaviour :

value1
value3

application.properties

category1.subcategory2=value1
category1.subcategory3=value3
greeting.salutation=Hello

TryValueApplication.java

package com.example.tryvalue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TryValueApplication {
    @Value("${category1.subcategory2}")
    private String category1;

    @Value("basic value")
    private String category3;

    public static void main(String[] args) {
        SpringApplication.run(TryValueApplication.class, args);
        TryValueApplication tryValueApplication = new TryValueApplication();
        System.out.println("Running main method");
        tryValueApplication.printCategory();
    }

    public void printCategory(){
        System.out.println("Print environment values");
        System.out.println(category1);
        System.out.println(category3);
    }
}

enter image description here

Tried : I tried accessing same value from within @RestController class and it was working . GreetingController.java

package com.example.tryvalue.controller;

import com.example.tryvalue.EnvironmentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
    @Value("${greeting.salutation}")
    private String greetingSalutation;

    @Autowired
    private EnvironmentValue environmentValue;

    @GetMapping
    public String greeting(){
        return greetingSalutation   " world";
    }

    @GetMapping("/category1")
    public String getCategory1(){
        return environmentValue.getCategory1();
    }
}

EnvironmentValue.java

package com.example.tryvalue;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EnvironmentValue {
    @Value("${category1.subcategory2}")
    private String category1;

    @Value("${category1.subcategory3}")
    private String category2;

    @Value("basic value")
    private String category3;

    public EnvironmentValue(){
        System.out.println("Creating instance of EnvironmentValue");
    }

    public String getCategory1(){
        return category1;
    }

    public String getCategory2(){
        return category2;
    }

    public String getCategory3(){
        return category3;
    }
}

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.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>tryValue</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tryValue</name>
    <description>tryValue</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

enter image description here

What can be the reason behind this difference in behaviour ? Thanks .

CodePudding user response:

You are creating a second unmanaged instance of TryValueApplication in your main method so the properties won't be injected into that.

The managed instance of TryValueApplication should have the values injected. Add this to TryValueApplication to print the values
    @Value("${category1.subcategory2}")
    private String category1;

    @Value("${category1.subcategory3}")
    private String category3;

    public static void main(String[] args) {
        SpringApplication.run(TryValueApplication.class, args);
    }

    @PostConstruct
    public void printCategory(){
        System.out.println("Print environment values");
        System.out.println(category1);
        System.out.println(category3);
    }
  • Related