@Value(“${welcome})在Spring Boot中抛出了非法参数异常并且无法解析占位符。

huangapple 未分类评论48阅读模式
英文:

@Value("${welcome}) in springboot is throwing illegal argument exception and could not resolve placeholder

问题

@RestController
Class MyController{

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

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

}
application.properties:
welcome=welcomeEla

以上代码抛出了非法参数异常,并显示无法解析值中的占位符'welcome',其值为"${welcome}"。

如果我在'$'和'{'之间加入一个空格,就不会抛出任何异常,但它会获取null值。

我还尝试过将环境变量与@PropertySource("application.properties")一起用作类上的autowired,但它也获取了null值(env.getProperty("welcome"))。

有谁可以帮我解决这个问题吗?提前致谢

英文:
@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

答案1

得分: -1

首先,

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

你运行这段代码。确保它正常工作。如果它正常工作,你可以尝试使用 @Value("${welcome.toString()}") 吗?

英文:

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

huangapple
  • 本文由 发表于 2020年5月29日 22:52:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/62088729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定