Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Value annotation always returning null

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);
        ...
    }
}
like image 212
NatureWonder Avatar asked Dec 05 '25 20:12

NatureWonder


1 Answers

Just to be clear.

@Component
@Getter
public class SampleComponentClass {

  @Value("${profile.name}")
  private String profileName;

}

@Component
Class SomeOtherClass{
    @Autowired
     SampleComponentClass sampleComponentClass;

    public void someMethod(){
     System.out.println(sampleComponentClass.getProfileName()); // prints testprofile
     SampleComponentClass sampleComponentClass1 = new SampleComponentClass();
     System.out.println(sampleComponentClass1.getProfileName()); // prints null
    }
}

In application.properties

profile.name=testprofile
like image 85
pvpkiran Avatar answered Dec 08 '25 09:12

pvpkiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!