How to change/update/delete a property in ConfigurableEnvironment of Spring
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
spring spring-mvc
asked Sep 25 '14 at 8:21
JOKe
1,01611126
1,01611126
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));
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
add a comment |
up vote
0
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));
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
add a comment |
up vote
0
down vote
// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));
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
add a comment |
up vote
0
down vote
up vote
0
down vote
// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));
// ConfigurableEnvironment env
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put(myObject.getKey(),
myObject.getQuery());
propertySources
.addFirst(new MapPropertySource("newmap", map));
answered Apr 27 '17 at 9:47
user6631150
11
11
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
add a comment |
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
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 19 at 21:45
mgibson
5924
5924
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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