Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Could not Resolve placeholder

Tags:

java

spring

I'm fairly new to spring so excuse me if this is a dumb question. When I try to launch a program I get the following error: java.lang.IllegalArgumentException: Could not resolve placeholder 'appclient' in string value [${appclient}]. The error is thrown when the following code is executed:

package ca.virology.lib2.common.config.spring.properties; import ca.virology.lib2.config.spring.PropertiesConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource;  @Configuration @Import({PropertiesConfig.class}) @PropertySource("${appclient}") public class AppClientProperties { private static final Logger log = LoggerFactory.getLogger(AppClientProperties.class); {     //this initializer block will execute when an instance of this class is created by Spring     log.info("Loading AppClientProperties"); } @Value("${appclient.port:}") private int appClientPort;  @Value("${appclient.host:}") private String appClientHost;  public int getAppClientPort() {     return appClientPort; }  public String getAppClientHost() {     return appClientHost; } } 

A property file called appclient.properties exists in the resources folder with the information for host and port. I'm not sure where the "${appclient}" is defined, if it is at all. Maybe it is not even defined and that is causing the problem. Do I need to change the "${appclient}" to something like "{classpath:/appclient.properties}" or am I missing something else?

like image 753
Brkk Avatar asked Feb 03 '15 19:02

Brkk


People also ask

How do I fix placeholder Cannot resolve?

Solution 2Check the application. properties file or system environment properties with the name of the key specified in the @Value annotation in the Java Spring Boot class. If the name of the property does not exist or varies from the required one, correct the name of the property key as per the java class.

What is PropertyPlaceholderConfigurer in spring?

The PropertyPlaceholderConfigurer is a property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

What is a Propertysourcesplaceholderconfigurer used for?

PropertyPlaceholderConfigurer is used to resolve ${...} placeholders against a property. It can be local properties or system properties or environment variables. We can use PropertyPlaceholderConfigurer using XML as well as annotation. PropertyPlaceholderConfigurer externalizes the property configuration.

What is application properties in spring boot?

Properties File Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. The application.properties file is located in the src/main/resources directory.


1 Answers

You are not reading the properties file correctly. The propertySource should pass the parameter as: file:appclient.properties or classpath:appclient.properties. Change the annotation to:

@PropertySource(value={"classpath:appclient.properties"}) 

However I don't know what your PropertiesConfig file contains, as you're importing that also. Ideally the @PropertySource annotation should have been kept there.

like image 83
Rohit Jain Avatar answered Sep 19 '22 13:09

Rohit Jain