how to define not mandatory property in spring?

前端 未结 2 1901
孤城傲影
孤城傲影 2020-12-14 15:10

I\'m using spring 3 with PropertyPlaceholderConfigurator.

My properties code looks like as following:

@Configuration
public class MyProps {

    @Val         


        
相关标签:
2条回答
  • 2020-12-14 15:51

    You could use a default value:

    @Value("${prop1:}")
    public String prop1;
    

    and spring will inject an empty string if the property isn't defined. The syntax is ${property:defaultValue}.

    0 讨论(0)
  • 2020-12-14 15:51

    I'm not sure if it is possible to make a single property optional but surely you can force the property placeholder to ignore unresolved properties:

    <context:property-placeholder ignore-unresolvable="true" ... />
    
    0 讨论(0)
提交回复
热议问题