-
Jan 17th, 2013, 12:55 PM
#1
@Value not working in web controller (but getProperty() does)
Hi,
I need to add a bunch of Properties backed in the DB at start up time.
To test the whole thing works, I started with this (the ds.username property below comes from catalina.properties. It's there just to verify I don't break anything):
public class PropertiesInitializer implements ApplicationContextInitializer<ConfigurableWebAppli cationContext> {
@Override
public void initialize(ConfigurableWebApplicationContext ctx) {
try {
props.put("hello", "goodbye");
MutablePropertySources propertySources = ctx.getEnvironment().getPropertySources();
propertySources.addFirst(new MapPropertySource("dbProps", props));
}
catch(Exception e) {
e.printStackTrace();
}
}
I have a @Controller and I'm doing this:
@Autowired
Environment env;
@Value( "${hello}" )
public String hello;
@Value( "${ds.username}" )
public String un;
...
System.out.println(hello + " " + un + " " + env.getProperty("hello") + " " + env.getProperty("ds.username"));
So, hello and un are "" but the env.getProperties actually return the right values.
Why?
Thanks
--
Gerardo Blanco
-
Jan 18th, 2013, 12:45 PM
#2
Please use [ code][/code ] tags when posting code, that way it remains readable...
Please post your configuration and actual controller code instead of snippets, hard to determine what (and if) there is something wrong.
-
Jan 18th, 2013, 12:47 PM
#3
Thanks for the 'code' tip. Wasn't sure how to do that.
As for the answer: I got a reply in StackOverflow. Someone said I needed to use the PropertySourcePlaceholderConfigurer for @Value to work. I implemented it, and I'm good.
Thanks
--
Gerardo Blanco
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules