13

I am trying to load a properties file into a Spring bean and then inject that bean into a class.

The only part I can't get to work seems to be using the @Resource reference.Can someone connect the last piece for me? I get a null value every time. Doesn't seem to want to inject the value.

[EDIT] - I originally thought using the @Resource was the best way but the proposed solution I found easier.

I saw this solution in another post:

Reference Solution Link: Inject Property Value into Spring - posted by DON

Credit to Don for the post but I just wasn't sure how to finish it with the @Resource.

Debugging Results: The variable value appProperties is always null. It's not being injected.

Spring Config. enter image description here

Sample Class:

package test;

import java.util.Properties;
import javax.annotation.Resource;


public class foo {
    public foo() {}
    @Resource private java.util.Properties appProperties;
}

Based on the advice in the approved solution below. Here are the changes I made.


Solution Update:

Spring Config: enter image description here

Java Class: enter image description here

Community
  • 1
  • 1
haju
  • 1,278
  • 4
  • 20
  • 38
  • What happens when you try your solution? – skaffman Jun 21 '11 at 13:17
  • private @Resource Properties is a typo? have you tried @Resource private Properties? I am not sure it shouldn't work like this I just never seen the annotation not before the modifier. – abalogh Jun 21 '11 at 13:54
  • @Skaffman - I get a null value. The variable value is not being binded and injected. – haju Jun 21 '11 at 14:30
  • @abalogh Ya I switch the @Resource around but still no change. – haju Jun 21 '11 at 14:46
  • Are you sure your foo bean is Spring-managed? is it explicitly defined in the appcontext or you are using component-scan? – abalogh Jun 21 '11 at 14:50
  • @abalogh - No I don't have a spring-managed foo bean. Do I need to define the a Foo bean in order to have that value defined? I thought maybe with the @Resource annotation I wouldn't need to. I could inject my appProperties bean. – haju Jun 21 '11 at 16:09

2 Answers2

17

For your solution to work you would also need to make foo a Spring managed bean; because otherwise how would Spring know that it has to deal with any of your annotations on your class?

  • You can either specify it in your appcontext xml as a bean with ..class="foo"
  • Or use component-scan and specify a base package which contains your foo class.

Since I'm not entirely sure this is exactly what you want (don't you want a .properties file to be parsed by Spring and have it's key-value pairs available instead of a Properties object?), I'm suggesting you another solution: Using the util namespace

<util:properties id="props" location="classpath:com/foo/bar/props.properties"/>

And reference the values inside your beans (also, have to be Spring managed):

@Value("#{props.foo}")
public void setFoo(String foo) {
    this.foo = foo;
}

EDIT:

just realized that you are importing org.springframework.context.ApplicationContext in your class which is probably unnecessary. I strongly encourage you to read Spring reference at least the first few chapters, because a) it's a great read b) you will find it much easier to understand Spring if the basics are clear.

abalogh
  • 8,239
  • 2
  • 34
  • 49
  • Thanks abalough I will try that with the – haju Jun 22 '11 at 12:48
  • If I choose the first method of implementation not using the @Component with component-scan, I wouldn't use the @Value anymore correct because I would have to inject in the spring config? – haju Jun 22 '11 at 19:04
  • Also note that the value you give inside @Value, "#{props.foo}" in the above example, should not contain dots for property KEY itself ("foo" in this example). I was migrating to use EL from old school way where my keys inside the property file were of the form a.b.c=xyz etc. And it failed. Whereas, say abc=xyz worked. – arcseldon Aug 21 '13 at 03:50
  • 2
    If you really do need the dots, then use the id[key] notation instead. example: app.billing.orderform=myorderform then set up properties id with say appProps. So the @Value becomes @Value("#{appProps['app.billing.orderform']}") I know, ugly isn't it. – arcseldon Aug 21 '13 at 04:07
3

Just one more solution using properties placeholder.

The spring context:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">

    <context:component-scan base-package="your.packege" />

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

</beans>

The java class where you want inject properties values:

public class ClassWithInjectedProperty {

    @Value("${props.foo}")
    private String foo;
}
alexey28
  • 5,170
  • 1
  • 20
  • 25