change a config.properties file in a jar / war file in runtime and hotdeploy the changes?

柔情痞子 提交于 2019-12-06 09:11:07

I will strongly recommend your architecht rethink this solution. What you describe should be done through JNDI or a similar technique, not through reloading properties.

Deployments should be considered static - that any given web container allows for magic trickery should not be depended on, and WILL break some day (most likely at the most inconvenient time).

You've got a couple of problems off the top of my head:

  1. ensuring that nothing is holding static references to a java.util.Properties that has previously loaded your config.properties file.

  2. most servlet engines will unpack your war to a working directory so the properties file you load won't be the one in the war, it will be the unpacked one. This means your changes will be overwritten when you restart the servlet engine because this is typically one of the points the war is unpacked.

While these problems aren't insurmountable I've always found it much easier to implement this sort of behavior by storing the properties in JNDI (as Thorbjørn suggests) or a database (while being careful about the static references I mentioned in point 1).

The JNDI/database solution has the nice side effect of easing deployment into multiple environments because each typically has it's own registry/database.

Even that I agree with the comments explained before, I could suggest one solution:

Apache Commons Configuration extension gives you the posibility to do something like:

config.setReloadingStrategy(new FileChangedReloadingStrategy());

That could make the trick to change the configuration file on a runtime basis with no code at all.

However, like JNDI and other methods of web application configuration, the security is a concern. Be careful on which parameters you can/must be able to configure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!