-1

application.properties file has:

my.greeting = Hello

ServiceClass file has:

@Value("${my.greeting}")
private String messageFromProperties;

MainAppfile:

public static void main(String[] args) {
        SpringApplication.run(SpringBootApplicationConceptsApplication.class, args);
        ServiceClass serv = new ServiceClass();`enter code here`
        System.out.println(serv.getMessageFromProperties());//getting null
    }

Please let me know what am i missing here. What all configuration is pending

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Kap
  • 1

1 Answers1

0

You need to use @PropertySource to specify the source. For example:

@PropertySource(value = "example.properties")

You can use the above-mentioned annotation with your main class. For example:

@SpringBootApplication
@PropertySource(value = "example.properties")
public class ExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

Or, you can use them with classes annotated with @Configuration. For example:

@Configuration
@PropertySource("example.properties")
public class ExampleConfiguration {
    //code
}

If this doesn't help, I would advise you to share your complete code for everyone to understand what exactly the problem is.