1

I have to read urls from a properties file, so that I don't have to go in code every time url is changed. I am using spring-maven project. I am using <context:property-placeholder location="classpath:apiUrls.properties"/> in my application-context.xml file. When I try to access property in java class with @Value("${my.property}") private String url; I am literally getting "${my.property}" in url instead of the value of property.

Here is a link: How to read values from properties file?

I am trying it with xml with the first method of PropertyPlaceholderConfigurer.

In web.xml file:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>

In applicationContext.xml file:

    <context:component-scan base-package="com.api"></context:component-scan>

    <context:property-placeholder location="classpath:apiUrls.properties"/>

In properties file(apiUrls.properties file): category.getAll=http://google.com/getSomeData

In java class:

@service
public class DemoCMSCategoryServiceImpl implements DemoCMSCategoryService {

    @Autowired
    RestTemplate restTemplate;

    @Value("${category.getAll}")
    private String url;
    .........
    .........
//In a function
String uri = this.url; //uri is ${my.property} not the value

This should have worked, I have done it in other projects. I also tried using @PropertySource(Nothing to do in xml then) and it worked, so the problem seems to be in xml part. Somehow the properties are not being added to the context. If it works properly then uri should be http://google.com/getSomeData -from the properties file. The Directory Structure of project is: Directory Structure

0 Answers0