How to programmatically resolve property placeholder in Spring

后端 未结 6 1469
借酒劲吻你
借酒劲吻你 2020-12-04 19:17

I currently work on a web application based on Spring 3.1.0.M1, annotations based, and I have a problem with resolving property placeholders in one specific place of my appl

6条回答
  •  离开以前
    2020-12-04 20:00

    One option is to add a PropertySource (here MapPropertySource to exemplify an in-memory configuration) to a ConfigurableEnvironment and ask it to resolve properties for you.

    public class Foo {
    
        @Autowired
        private ConfigurableEnvironment env;
    
        @PostConstruct
        public void setup() {
            env.getPropertySources()
               .addFirst(new MapPropertySource("my-propertysource", 
                   ImmutableMap.of("your.property.name", "the value")));
            env.resolvePlaceholders("your.property.name");
        }
    }
    

    Optionally annotate the Foo class with @Configuration to enjoy the power of programmatic configuration in favor of XML.

提交回复
热议问题