Announcement Announcement Module
Collapse
No announcement yet.
How to define constants from a properties file with spring 3.1 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to define constants from a properties file with spring 3.1

    Hello,

    Maybe this subject has already beeen discussed here, but i can't find a simple solution to make this work.
    Here is what iwant: a class defining constants values that can be overridden by a properties file:

    For this, my spring context configuration is as follow:
    Code:
    <context:annotation-config/>
    <context:component-scan base-package="xxxxx"/>
    <context:property-placeholder/>
    Code:
    public class TestConstants {
    
    	public static String constant1;
    	public static int constant2;
    }
    and my properties
    Code:
    prop.test1=test3
    prop.test2=2
    I've read a lot of things about properties with spring 3.0 and 3.1 so i tried to do what seems to be the most simple for me:
    Code:
    @Configuration
    @PropertySource("classpath:test.properties")
    public class TestConstants {
            @Value("${prop.test1:default}")
    	public static String constant1;
            @Value("${prop.test2:1}")
    	public static int constant2;
    }
    this doesnt work as the fields are declared static. If i remove the 'static', it works, but i cant access them in a static way, i must instanciate the TestConstants before and use getters.

    A solution tu use them in a static way seems to be:
    Code:
    @Configuration
    @PropertySource("classpath:test.properties")
    public class TestConstants {
            
    	public static String constant1;
    	public static int constant2;
            
            @Autowired(required=true)
            public void setConstant1(@Value("${prop.test1:default}")String constant1)
            {
                 TestConstants.constant1 = constant1;
            }
    
            @Autowired(required=true)
            public void setConstant2(@Value("${prop.test2:1}")String constant2)
            {
                 TestConstants.constant1 = Integer.parseInt(constant2);
            }
    }
    Is there another solution ? simpler than this one, to avoid having 50 setters if i have 50 constants ?
    Maybe i missed something with Spring 3.1 ?

    Another possibility is to define a Properties object with <util: properties id="props" location="classpath:test.properties" /> and inject it in my TestConstants class with @Autowired and @Qualifier("props") but i need to manually affect all my fields with the values of the Properties object.
    But i think if i have to do that, i dont need spring and i can do it in the same way with a simple properties.load(...)

    Thanks in advance for your suggestions

  • #2
    Currently Spring does not support static injection. Spring can't help you much in your case. So if you want static fields in your bean, do the same way you mentioned (with 50 setters ). Or think about using non-static properties in your TestConstants class. You don't need instantiate it before use 'cause Spring will do that for you. Inject your TestConstants and use it with getters.

    Comment

    Working...
    X