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/>and my propertiesCode:public class TestConstants { public static String constant1; public static int constant2; }
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:prop.test1=test3 prop.test2=2
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.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; }
A solution tu use them in a static way seems to be:
Is there another solution ? simpler than this one, to avoid having 50 setters if i have 50 constants ?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); } }
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![]()


Reply With Quote
). 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.
