Results 1 to 3 of 3

Thread: @Value not working in web controller (but getProperty() does)

Hybrid View

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Default @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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2012
    Posts
    3

    Default

    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
  •