Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject property to spring bean using annotation

As explained here and here it is quite clear how to do it but still can't seem to make it work. I simply like to use the @Value annotation in order to inject a property to a spring bean. I created a basic spring MVC project with one controller and one bean.

Here is my application context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">


<!-- Root Context: defines shared resources visible to all other web components -->

<context:component-scan base-package="me.co.fatsecret" />

<!-- Properties -->

<bean id="props"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:fatProperties.properties" />
</bean>


</beans>

I have one bean called Configuration:

package me.co.fatsecret;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Configuration {

    /*--- Members ---*/

    @Value("${api_key}")
    protected String API_KEY;
    @Value("${api_secret}")
    protected String API_SECRET;
    @Value("${api_url}")
    protected String API_URL;

    /*--- Constructors ---*/

    public Configuration() {
    }

    /*--- Getters & Setters ---*/

    public String getAPI_KEY() {
    return API_KEY;
    }

    public void setAPI_KEY(String aPI_KEY) {
    API_KEY = aPI_KEY;
    }

    public String getAPI_SECRET() {
    return API_SECRET;
    }

    public void setAPI_SECRET(String aPI_SECRET) {
    API_SECRET = aPI_SECRET;
    }

    public String getAPI_URL() {
    return API_URL;
    }

    public void setAPI_URL(String aPI_URL) {
    API_URL = aPI_URL;
    }

}

Now I have only one controller, injected with this Configuration class and as I call this controller I see that the values in the Configuration class are not populated right.

My properties file is located under the resources folder (src/main/resources) and is a part of my classpath (done by default since this is a maven project). Here it is:

api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret

The file name is fatProperties.properties. As I debug my server when calling the controller I see that the content of the Configuration class is:

${api_key}
${api_secret}
${api_url}

This is the actual value of the Strings, wich means that the vales from the properties file are not getting injected for some reason.

Am I missing something here?

UPDATE1: I replaced the PropertyPlaceholderConfigurer bean with:

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

Getting the same result

like image 554
forhas Avatar asked Mar 04 '26 06:03

forhas


2 Answers

Ok, got it!

I'm using a spring MVC project, which means I have a separated context for my web layer (the controllers). The "Configuration" bean which hods the properties using the @Value annotation is injected to a controller. My property-placeholder is defined within my root-context hence it cannot be seen from my controller. To resolve the issue I simply added the property-placeholder definition to my DispatcherServlet context and it works like a charm :)

like image 111
forhas Avatar answered Mar 06 '26 21:03

forhas


Add this to your application context file:

<context:property-placeholder location="classpath:fatProperties.properties" />
like image 33
nickdos Avatar answered Mar 06 '26 21:03

nickdos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!