<context:property-placeholder location="classpath:context-core.properties"/>
By default a PlaceholderConfigurer
is going to fail-fast, so if a placeholder cannot be resolved it will throw an exception. The instance from the applicationContext.xml
file has no properties and as such will fail on all placeholders.
Solution: Remove the one from applicationContext.xml as it doesn't add anything it only breaks things.
–
–
I got same error in my Micro-service, whenever you declare @Value annotation in program i.e @Value("${project.api.key}")
make sure that your application.properties file with same values should not be blank project.api.key= add some values
MostIMP :otherwise it will throw error "Error creating bean with name 'ServiceFTP': Injection of autowired dependencies"
–
–
This Issue occurs if the application is unable to access the some_file_name.properties file.Make sure that the properties file is placed under resources folder in spring.
Trouble shooting Steps
1: Add the properties file under the resource folder.
2: If you don't have a resource folder. Create one by navigating new by Right click on the project new > Source Folder, name it as resource and place your properties file under it.
For annotation based Implementation
Add @PropertySource(ignoreResourceNotFound = true, value = "classpath:some_file_name.properties")
//Add it before using the place holder
Example:
Assignment1Controller.Java
@PropertySource(ignoreResourceNotFound = true, value = "classpath:assignment1.properties")
@RestController
public class Assignment1Controller {
// @Autowired
// Assignment1Services assignment1Services;
@Value("${app.title}")
private String appTitle;
@RequestMapping(value = "/hello")
public String getValues() {
return appTitle;
assignment1.properties
app.title=Learning Spring
–
You can also try default values. spring-value-annotation
Default values can be provided for properties that might not be defined. In this example the value “some default” will be injected:
@Value("${unknown.param:some default}")
private String someDefault;
If the same property is defined as a system property and in the properties file, then the system property would be applied.
–
In my case, I was careless while merging the application.yml file, and I've unnecessary indented my properties to the right.
I've indented it like this:
spring:
application:
name: applicationName
............................
myProperties:
property1: property1value
While the code expected it to be like this:
spring:
application:
name: applicationName
.............................
myProperties:
property1: property1value
In my case I had the same issue on running from eclipse. Just did the following to resolve it:
Right Click the Project --> Mavan --> Update Project.
And it worked!
–
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
Example in class Java
@Configuration
@Slf4j
public class MyAppConfig {
@Value("${foo}")
private String foo;
@Value("${bar}")
private String bar;
@Bean("foo")
public String foo() {
log.info("foo={}", foo);
return foo;
@Bean("bar")
public String bar() {
log.info("bar={}", bar);
return bar;
[ ... ]
In the properties files :
src/main/resources/application.properties
foo=all-env-foo
src/main/resources/application-rec.properties
bar=rec-bar
src/main/resources/application-prod.properties
bar=prod-bar
In the VM arguments of Application.java
-Dspring.profiles.active=[rec|prod]
Don't forget to run mvn command after modifying the properties !
mvn clean package -Dmaven.test.skip=true
In the log file for -Dspring.profiles.active=rec :
The following profiles are active: rec
foo=all-env-foo
bar=rec-bar
In the log file for -Dspring.profiles.active=prod :
The following profiles are active: prod
foo=all-env-foo
bar=prod-bar
In the log file for -Dspring.profiles.active=local :
Could not resolve placeholder 'bar' in value "${bar}"
Oups, I forget to create application-local.properties.
This error appears because the spring project doesn't read the file properties (bootstrap.yml or application.yml). In order to resolve this, you must add dependency in your pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
I´m frequently running into this issue on some custom properties that could not be found using IntelliJ IDEA - likely after changing branches.
What helpes in my case is
File -> Invalidate Caches / Restart
I had the assumption that it is more likely a Gradle caching issue than an IDE issue, but ./gradle clean did not help
–
I run into this when I created an @Configuration
class with a prefix e.g. foo.bar
then went ahead to attempt to autowire a property in another class using @Value("${foo.bay.xyz}")
.
The solution was to add the xyz
property to the configuration class and access it from an autowired bean of that class.
I could not find the reason for this in the docs but my guess is that spring was binding all properties under the foo.bar
prefix under the configuration class.
When you're using Spring Boot with spring-boot-starters and you redefine variables in .yml / .properties file, make sure you using custom names for variables.
For example you're using spring-boot-starter-mail. And defining variables from mail-starter in the .yml file next way:
spring:
mail:
host: ${my_smtp_host}
In this case you'll get an IllegalArgumentException: Could not resolve placeholder 'my_smtp_host' in string value "${my_smtp_host}"
To solve this issue just make custom variable. For example:
my-mail:
host: ${my_smtp_host}
Another note - if you're using Openshift make sure your variable my_smtp_host
not starting from digit:
change this -> ${123_my_smtp_host}
to this -> ${my_smtp_host}
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.