java - Spring override properties at runtime and keep them persistent -
I am using Spring 3.2.8 and keep my settings in a property file. Now I want to override some of them over runtime, I want to overwrite the old values in the properties file and keep the new values constant.
How can I do this in the spring? Some properties I inject with @value
and get others with MessageSource.getMessage (string, object [], locale). Beans are instantiated with these values already. How can I access properties, store them, and update all the beans systems?
Thank you!
OK, when I give your follow-up answer, I will keep it simple enough and before spring Only you will know. I will make some assumptions that the annotation configuration is right for you.
In my example, I will consider all of the properties that you want to configure which
Is read on ServerConfiguration
and initially
Then part 1, I define a bean named ServerProperties
which is the value in the injection value from server.properties
.
Such:
@Component public class ServerProperties {@Value ("$ {server .IP}}"); Private string ipAddress; ... Public Zero Setupdress (String IPadress) {this.ipAddress = ipAddress; } Public string getIpAddress () {return this.ipAddress; }
}
Second, depending on these properties, I use @ value
instead of using ServerProperties
Code> example:
@Component public class configuration {@ Autoward Private Server Properties Server Properties; @PostConstruct Public Zero init () (if serverProperties.getIpAddress (.) Equals ("localhost") {...} else {...}}}
Third, I will disclose A simple controller
in which serverproperties
is injected so that you can use your web page to update system properties eg:
< Code> @Controller public class UpdateProperties {@AutoWired Private Server Properties Server References; @RequestMapping ("/ updateProperties") Public String UpdateProperties () {ServerProperties.setIpAddress (...); Return "Full";}
Finally,
@Component public class when I close To flush the existing property value, I will use the
onServerProperties @PreDestroy
to open ServerProperties {@PreDestroy Public Zero () {...} Open the file and type P Roperties for the server. Properties}}
You should give a framework for what you need. I'm sure it can be touched, but you will find it there.
Comments
Post a Comment