-1
@RestController
Class MyController{

@Value("${welcome}")
private String welcome;

@RequestMapping("/greet")
public String greetHim(){
  return welcome;
}

}
application.properties:
welcome=welcomeEla

The above code is throwing illegal argument exception and saying could not resolve placeholder 'welcome' in value "${welcome}"

If I give a space between '$' and '{' it is not throwing any exception but it is fetching null value.

Also I tried with environment variable as autowired with @PropertySource("application.properties") over the Class it was also fetching null value (env.getProperty("welcome"))

Can anybody help me in this? Thanks in advance

Ela
  • 91
  • 6
  • 1
    Where have you placed your application.properties? Is it loaded when the app is started? – aksappy May 29 '20 at 15:01
  • If you're splitting the `${`, you shouldn't be getting `null`. You should literally get the `String` value that you specified, ie. `"$ {welcome}"`, because there's no longer a placeholder there to resolve. Please provide a [mcve]. – Savior May 29 '20 at 15:15
  • application.properties file was created automatically and placed under resources folder. Sry When i was trying with Autowired Environment variable, ithe output was null. Trying with @Value("$ {welcome}") outputs "$ {welcome}" itself Reproducable example: ```@RestController public class MyController { @Value("$ {welcome}") private String welcome; @RequestMapping("/greet") public String greetHim() { System.out.println("Welcome : " + welcome); return welcome; } } Output: $ {welcome}``` – Ela May 29 '20 at 16:03
  • We need the [mcve] for your entire use case. How you setup your application, how it's configured and run. Provide the full stack trace as well. – Savior May 29 '20 at 17:02

1 Answers1

-1

Firstly ,

ApplicationContextProvider appContext = new ApplicationContextProvider();
Environment env = appContext.getApplicationContext().getEnvironment();
// env.getProperty() works!!!
String temp = env.getProperty("welcome");
System.out.println(temp) ;

you run this code. And make sure it works. Do you try @Value("${welcome.toString()}") if it works

Sencer Seven
  • 126
  • 1
  • 10
  • I tried your code. But still it is giving output as null. Following is the code i tried. ```@RestController @PropertySource("classpath:application.properties") public class MyController { @Autowired ApplicationContextProvider applicationContextProvider; @RequestMapping("/greet") public String greetHim() { Environment env = applicationContextProvider.getApplicationContext().getEnvironment(); String temp = env.getProperty("welcome"); System.out.println(temp) ; return temp; } }``` – Ela May 29 '20 at 17:01
  • The below is the ApplicationContextProvider. ```@Component public class ApplicationContextProvider implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public ApplicationContext getApplicationContext() { return applicationContext; } }``` – Ela May 29 '20 at 17:02
  • Do you try ```@Value("${welcome.toString()}")``` if it works it is throwing ```illegal argument exception and could not resolve place holder````. if i give space between '$ and '{' it is giving output as ```$ {welcome.toString()}``` – Ela May 29 '20 at 17:18