0

I have the following code snippet. I wanted to read a property from appconfig.properties file

public final class class1 implements interface1
{
   @Value("${test.url}")
   private String baseUrl;
  //This URL is different for different environments

  public class1(ClassC2 c2, ClassC3 c3) {
  // Some initialization code
  }

}

baseUrl is always null. How could I inject the property in this scenario?

Enviroment - Java 11, springframework version - 5.3.19

Sravya
  • 21
  • 5

2 Answers2

0

you cannot use @Value for non managed spring classes, meaning classes that use @Value must be annotated by one of followings @Service, @Component, @Repository, @RestController, and @Configuration.

Note that the managed classes should be also injected for its usages and not declaring with new operator

Mohammed Fataka
  • 150
  • 1
  • 8
  • Thank you for your response. Without using @Value, is there really a way to inject? – Sravya Apr 18 '23 at 00:31
  • yes but depends on your logic of code, can you explain why you do not want to inject? or how is your code logic? – Mohammed Fataka Apr 18 '23 at 00:59
  • I just wanted to read the property from the file into this class. Nothing specific. There is an API in this class which makes HTTP calls, the endpoints are different for different enviroments (pre-prod, local and prod etc), so I am trying to define those endpoints in props file, so i could make the calls to appropriate URLs – Sravya Apr 18 '23 at 01:28
  • 1
    it is possible to do it without running your application in spring check this question https://stackoverflow.com/questions/8285595/reading-properties-file-in-java , or this https://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app – Mohammed Fataka Apr 18 '23 at 01:57
  • With the loader approach, I am unsure how to refactor the code, because this class will be loaded for each specific object (the object will be instantiated based on different parameters within). But, I wanted to load the property only once for the entire application as the url won't change for each object – Sravya Apr 18 '23 at 03:19
0

You can have a property declared in your application.properties as

spring.profiles.active=dev
test.url=${TEST_URL}

then create different configuration properties files based on your environment such as application-dev.properties or application-prod.properties and define TEST_URL in this files

application-dev.properties

TEST_URL=some_test_url_for_dev

application-prod.properties

TEST_URL=some_test_url_for_prod

Spring Boot has inbuilt support for the environment or profile-specific Application Properties. That means we can have multiple application properties , and Spring will refer to the file that applies to the currently active profile.

The default properties file in a Spring Boot application is ‘application.properties‘. In addition, we can have profile-specific properties that follow the naming pattern – application-{spring.profiles.active}.properties. such as application-dev.properties

You can check spring profiles documentation spring profile documentation

Teletubbies
  • 43
  • 10
  • The application is not spring boot, there is a way to define props (similar to above), and I did that. But the problem is while reading those props. As the class is not really managed by Spring, I can't use @Value – Sravya Apr 18 '23 at 01:37
  • okay, have you tried to get it from Environment [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.html] class – Teletubbies Apr 18 '23 at 01:46
  • yes it did not wotk. I get error when used Autowired, it says Autowired members must be defined in valid spring benas – Sravya Apr 18 '23 at 02:09