I am new to Spring Boot and using Spring Annotations. I am trying to build a sample spring boot application in which I get a value for my java property using the properties file. I am trying to use @Component
& @ConfigurationProperties
parameter. I followed a bunch of tutorials online and this StackOverflow article helped, but my property value is still null
Here is my code.
Looked through:-
unable to read properties using configurationproperties annotation
https://www.mkyong.com/spring/spring-propertysources-example/
My SpringBoot Application class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@EnableAutoConfiguration
@SpringBootApplication
public class Main {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Main.class, args);
TestConf t = context.getBean(TestConf.class);
System.out.println(t.toString());
}
}
TestConf Class
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("pres")
public class TestConf {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public TestConf()
{
System.out.println("inside constructor");
System.out.println("first name:" + firstName);
}
public static void main(String[] args){
TestConf t = new TestConf();
}
@Override
public String toString() {
return "firstName:" + firstName;
}
}
application.properties
pres.firstName=JACQUELYN
lastly my 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>phil</groupId>
<artifactId>springTuit</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</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-thymeleaf</artifactId>
</dependency>
<!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
What is missing to retrieve the value from application.properties
correctly into firstName?
Does it matter where my application.properties
file lies? Although it is in its traditional spot under src/main/resources
and my java files are under src/main/java
This seems standard. However, you can try with the combination
@Component
@PropertySource("classpath:applciation.properties")
@ConfigurationProperties
to TestConf.Java