Can I replace a Spring bean definition at runtime?

后端 未结 11 1380
萌比男神i
萌比男神i 2020-11-30 17:44

Consider the following scenario. I have a Spring application context with a bean whose properties should be configurable, think DataSource or MailSender

11条回答
  •  鱼传尺愫
    2020-11-30 18:39

    Or you could use the approach from this similar question and hence also my solution:

    The approach is to have beans that are configured via property files and the solution is to either

    • refresh the entire applicationContext (automatically using a scheduled task or manually using JMX) when properties have changed or
    • use a dedicated property provider object to access all properties. This property provider will keep checking the properties files for modification. For beans where prototype-based property lookup is impossible, register a custom event that your property provider will fire when it finds an updated property file. Your beans with complicated lifecycles will need to listen for that event and refresh themselves.

提交回复
热议问题