How to change/update/delete a property in ConfigurableEnvironment of Spring












1















In Spring you can use inject an Environment object to read all environment properties



@Resource
private org.springframework.core.env.Environment environment;


So the question is can I programatically change an value of some property?



The only workaround I see is to get the all MutablePropertySource that holds this property. to remove this source completely from the environment and to add a new PropertySource that contains all properties of the previous one + the changed one (or the removed one).



However this looks ugly and will be slow ;(










share|improve this question























  • Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

    – varun
    Sep 25 '14 at 8:36











  • well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

    – JOKe
    Sep 25 '14 at 9:20











  • and this actually updates the value in property file?

    – varun
    Sep 25 '14 at 9:28
















1















In Spring you can use inject an Environment object to read all environment properties



@Resource
private org.springframework.core.env.Environment environment;


So the question is can I programatically change an value of some property?



The only workaround I see is to get the all MutablePropertySource that holds this property. to remove this source completely from the environment and to add a new PropertySource that contains all properties of the previous one + the changed one (or the removed one).



However this looks ugly and will be slow ;(










share|improve this question























  • Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

    – varun
    Sep 25 '14 at 8:36











  • well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

    – JOKe
    Sep 25 '14 at 9:20











  • and this actually updates the value in property file?

    – varun
    Sep 25 '14 at 9:28














1












1








1








In Spring you can use inject an Environment object to read all environment properties



@Resource
private org.springframework.core.env.Environment environment;


So the question is can I programatically change an value of some property?



The only workaround I see is to get the all MutablePropertySource that holds this property. to remove this source completely from the environment and to add a new PropertySource that contains all properties of the previous one + the changed one (or the removed one).



However this looks ugly and will be slow ;(










share|improve this question














In Spring you can use inject an Environment object to read all environment properties



@Resource
private org.springframework.core.env.Environment environment;


So the question is can I programatically change an value of some property?



The only workaround I see is to get the all MutablePropertySource that holds this property. to remove this source completely from the environment and to add a new PropertySource that contains all properties of the previous one + the changed one (or the removed one).



However this looks ugly and will be slow ;(







spring spring-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 25 '14 at 8:21









JOKeJOKe

1,03311227




1,03311227













  • Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

    – varun
    Sep 25 '14 at 8:36











  • well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

    – JOKe
    Sep 25 '14 at 9:20











  • and this actually updates the value in property file?

    – varun
    Sep 25 '14 at 9:28



















  • Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

    – varun
    Sep 25 '14 at 8:36











  • well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

    – JOKe
    Sep 25 '14 at 9:20











  • and this actually updates the value in property file?

    – varun
    Sep 25 '14 at 9:28

















Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

– varun
Sep 25 '14 at 8:36





Why you want to change a property anyway, properties are supposed to remain constant throughout. Still you can use AOP to do this, in your advice just the parameter value if it matches the key which you want to change just return the new value.

– varun
Sep 25 '14 at 8:36













well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

– JOKe
Sep 25 '14 at 9:20





well I want to give the ability to some "administrator" of my site to change the properties on the fly without restarting and without need to have access to the property file or the machine.

– JOKe
Sep 25 '14 at 9:20













and this actually updates the value in property file?

– varun
Sep 25 '14 at 9:28





and this actually updates the value in property file?

– varun
Sep 25 '14 at 9:28












2 Answers
2






active

oldest

votes


















2














// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));





share|improve this answer
























  • by this u can add or update a property within environment

    – user6631150
    Apr 27 '17 at 9:49











  • Could you please add some explanation to your answer ?

    – kaldoran
    Apr 27 '17 at 9:55






  • 1





    when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

    – user6631150
    May 12 '17 at 7:08



















0














Note that 'newmap' in the above answer by @user6631150 is the name of the property file where you want to update/add values.



Also not that this does not change the property file on disk, it only updates it in memory.



Meaning: if you have a property file newmap.properties located in C:/user/app_dir/newmap.properties and you modify it with the above code, you will not see changes in the file at this location. The changes will be in memory only. If your application restarts, no changes will be at the said location.






share|improve this answer
























  • Yes but this is what I was looking long time ago :D

    – JOKe
    Dec 7 '18 at 10:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26033779%2fhow-to-change-update-delete-a-property-in-configurableenvironment-of-spring%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));





share|improve this answer
























  • by this u can add or update a property within environment

    – user6631150
    Apr 27 '17 at 9:49











  • Could you please add some explanation to your answer ?

    – kaldoran
    Apr 27 '17 at 9:55






  • 1





    when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

    – user6631150
    May 12 '17 at 7:08
















2














// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));





share|improve this answer
























  • by this u can add or update a property within environment

    – user6631150
    Apr 27 '17 at 9:49











  • Could you please add some explanation to your answer ?

    – kaldoran
    Apr 27 '17 at 9:55






  • 1





    when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

    – user6631150
    May 12 '17 at 7:08














2












2








2







// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));





share|improve this answer













// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 27 '17 at 9:47









user6631150user6631150

363




363













  • by this u can add or update a property within environment

    – user6631150
    Apr 27 '17 at 9:49











  • Could you please add some explanation to your answer ?

    – kaldoran
    Apr 27 '17 at 9:55






  • 1





    when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

    – user6631150
    May 12 '17 at 7:08



















  • by this u can add or update a property within environment

    – user6631150
    Apr 27 '17 at 9:49











  • Could you please add some explanation to your answer ?

    – kaldoran
    Apr 27 '17 at 9:55






  • 1





    when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

    – user6631150
    May 12 '17 at 7:08

















by this u can add or update a property within environment

– user6631150
Apr 27 '17 at 9:49





by this u can add or update a property within environment

– user6631150
Apr 27 '17 at 9:49













Could you please add some explanation to your answer ?

– kaldoran
Apr 27 '17 at 9:55





Could you please add some explanation to your answer ?

– kaldoran
Apr 27 '17 at 9:55




1




1





when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

– user6631150
May 12 '17 at 7:08





when we got propertySouces from env( ConfigurableEnvironment). we are able to attach multiple properties within a new map object along with data u wanna add or update. and passing to propertySources method will allow u to add/update ur new property.

– user6631150
May 12 '17 at 7:08













0














Note that 'newmap' in the above answer by @user6631150 is the name of the property file where you want to update/add values.



Also not that this does not change the property file on disk, it only updates it in memory.



Meaning: if you have a property file newmap.properties located in C:/user/app_dir/newmap.properties and you modify it with the above code, you will not see changes in the file at this location. The changes will be in memory only. If your application restarts, no changes will be at the said location.






share|improve this answer
























  • Yes but this is what I was looking long time ago :D

    – JOKe
    Dec 7 '18 at 10:45
















0














Note that 'newmap' in the above answer by @user6631150 is the name of the property file where you want to update/add values.



Also not that this does not change the property file on disk, it only updates it in memory.



Meaning: if you have a property file newmap.properties located in C:/user/app_dir/newmap.properties and you modify it with the above code, you will not see changes in the file at this location. The changes will be in memory only. If your application restarts, no changes will be at the said location.






share|improve this answer
























  • Yes but this is what I was looking long time ago :D

    – JOKe
    Dec 7 '18 at 10:45














0












0








0







Note that 'newmap' in the above answer by @user6631150 is the name of the property file where you want to update/add values.



Also not that this does not change the property file on disk, it only updates it in memory.



Meaning: if you have a property file newmap.properties located in C:/user/app_dir/newmap.properties and you modify it with the above code, you will not see changes in the file at this location. The changes will be in memory only. If your application restarts, no changes will be at the said location.






share|improve this answer













Note that 'newmap' in the above answer by @user6631150 is the name of the property file where you want to update/add values.



Also not that this does not change the property file on disk, it only updates it in memory.



Meaning: if you have a property file newmap.properties located in C:/user/app_dir/newmap.properties and you modify it with the above code, you will not see changes in the file at this location. The changes will be in memory only. If your application restarts, no changes will be at the said location.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 21:45









mgibsonmgibson

5924




5924













  • Yes but this is what I was looking long time ago :D

    – JOKe
    Dec 7 '18 at 10:45



















  • Yes but this is what I was looking long time ago :D

    – JOKe
    Dec 7 '18 at 10:45

















Yes but this is what I was looking long time ago :D

– JOKe
Dec 7 '18 at 10:45





Yes but this is what I was looking long time ago :D

– JOKe
Dec 7 '18 at 10:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26033779%2fhow-to-change-update-delete-a-property-in-configurableenvironment-of-spring%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)