Apply dynamic properties to a bean at runtime

后端 未结 3 1945
无人共我
无人共我 2020-12-21 00:34

Assume I have a bean DialogBox, with properties for height and width:

public class DialogBox {
 int x;
 int y;
 ...
}

In my applicationCont

相关标签:
3条回答
  • 2020-12-21 01:22

    Take a look at the Spring OSGi Compendium services, they've got a property manager called "managed-properties", which allows you not only to update the properties at runtime, but while the application is running if you select the "container-managed" update strategy.

    0 讨论(0)
  • 2020-12-21 01:24

    If I understood the question, you can use a FactoryBean to customize bean creation logic in Spring.

    0 讨论(0)
  • 2020-12-21 01:40
    1. Use FactoryBean (as already suggested) to customize instantiation.
    2. set scope="prototype" on the bean, so that each time an instance is required, a new one should be created.
    3. In case you want to inject the prototype bean into a singleton bean, use lookup-method (Search for lookup-method here)

    I'm not sure if this would fit your case though. Another suggestion would be:

    In @PostConstruct methods of your various "clients" set the properties as desired on the already injected dialog window. Like:

    public class MyDialogClient {
        @Autowired
        private Dialog dialog;
    
        @PostConstruct
        public void init() {
            dialog.setWidth(150); //or read from properties file
            dialog.setHeight(200);
        }
        ...
    }
    

    Again, in this case, you can play with the scope atrribute.

    0 讨论(0)
提交回复
热议问题