Spring @Value annotation not using defaults when property is not present

后端 未结 3 2204
失恋的感觉
失恋的感觉 2021-02-07 01:24

I am trying to use @Value annotation in the parameters of a constructor as follows:

@Autowired
public StringEncryptor(
    @Value(\"${encryptor.password:\\\"\\\"         


        
3条回答
  •  伪装坚强ぢ
    2021-02-07 02:01

    ignore-resource-not-found="true" is not necessary for the defaults to be picked up. The point of specifying the default value is for it to be used if the property is not found anywhere.

    I think the last sentence in the previous answer points to the problem - incorrect EL that you must have originally provided but then removed from the example. The fact that you were getting format conversion exceptions points to that as well. Normally, Spring will automatically convert Strings to the appropriate "standard" Java type, and if you provide your own implementation of the Spring Conversion Service, to your custom objects as well - as long as your conversion service is defined in the app context.

    "ignore-resource-not-found" is useful when you are injecting properties via XML without defaults and don't want the container to throw an exception instantiating the bean in case no property is found. In such cases the bean properties will be initialized with the Java defaults, e.g. nulls fro objects, 0s for primitive numeric values, etc.

提交回复
热议问题