Announcement Announcement Module
Collapse
No announcement yet.
@Value not working in web controller (but getProperty() does) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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

  • #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.

    Comment


    • #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

      Comment

      Working...
      X