I'm trying to read the content in my application.properties file using the @Value annotation. But it will always return null back.
**Sample Java code**
@Component
public class SampleClass() implements Runnable{
@Value("${profile.name}")
private String name;
@Override
public void run(){
System.out.println("Name: " + name);
...
}
}
**Application.properties**
profile.name=myname
**Pom.xml**
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<id>stag</id>
<properties>
<activatedProperties>stag</activatedProperties>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
....
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
I've even tried giving a default value to the variable and it still returning null.
@Value("${profile.name:'samplename'}")
Why??
Update
I managed to retrieve the profile.name value in my SampleClass() now. But i'm not really sure if the way I did was the correct way. SampleClass1 will call SampleClass2.
Sample Java 1 Code
@Component
public class SampleClass1(){
@Autowired
SampleClass2 sampleClass2;
@Value("${profile.name}")
private String profileName;
sampleClass2.setName(profileName);
//Invoke SampleClass2
}
Sample Java 2 Code
@Component
public class SampleClass2() implements Runnable{
private String profileName;
public void setName(String profileName){
this.profileName= profileName;
}
@Override
public void run(){
System.out.println("Name: " + profileName);
...
}
}
run()
method? It looks like the instance of the SampleClass is not created by Spring. – Daniel Olszewskinull
, you either have a default value or your application blows up when starting. So you are probably usingnew SampleClass()
instead of using the spring configured instance. Also the fact that you are using maven profiles and resource filtering with spring boot worries me a little (in short you don't need it nor should want it, use the Spring Boot support with profiles). – M. Deinumnew SampleClass()
. what should be the correct way to deal with this? – NatureWonderApplication.properties
file located in your project? – carlspring