Spring Boot @ConditionalOnProperty does not work on external property source except application.properties
I am trying to dynamically enable/disable some service based on a flag, So I annotated my controller with @ConditionalOnProperty("ENABLE_API_COMPANY_SERVICE")
. If I store the property into applicaiton.properties
file then it works fine.
But whenever I try to read the property from external source like DB , it does not work. As per a different stack-overflow answer , the external property get loaded after the conditional property executed.
- I believe there should be some way in Spring Boot, this can be done using external property source like DB or Spring cloud Config. Anyone has any idea?
Now.... I tried using the @RefreshScope
annotation , by which one bean can be dynamically loaded without restarting the application. So I annotated my same controller with @RefreshScope
thinking that , if that controller can be re-initialized anytime after the application started, it should use the loaded property from external source. but this is also not working. Is this a stupid idea?
spring-mvc spring-boot
add a comment |
I am trying to dynamically enable/disable some service based on a flag, So I annotated my controller with @ConditionalOnProperty("ENABLE_API_COMPANY_SERVICE")
. If I store the property into applicaiton.properties
file then it works fine.
But whenever I try to read the property from external source like DB , it does not work. As per a different stack-overflow answer , the external property get loaded after the conditional property executed.
- I believe there should be some way in Spring Boot, this can be done using external property source like DB or Spring cloud Config. Anyone has any idea?
Now.... I tried using the @RefreshScope
annotation , by which one bean can be dynamically loaded without restarting the application. So I annotated my same controller with @RefreshScope
thinking that , if that controller can be re-initialized anytime after the application started, it should use the loaded property from external source. but this is also not working. Is this a stupid idea?
spring-mvc spring-boot
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implementEnvironmentPostProcessor
orApplicationContextInitializer
?
– jbarrueta
Nov 21 '18 at 18:39
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
You need to add the properties very early an not use an@PropertySource
or worse (as you have) add an additionalPropertyPlaceholderConfigurer
(this might even break your application!). Instead use either anApplicationContextInitializer
or theEnvironmentPostProcessor
to add the DB drivenPropertySource
. That way it will be part of theEnvironment
as it should be.
– M. Deinum
Nov 21 '18 at 19:45
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04
add a comment |
I am trying to dynamically enable/disable some service based on a flag, So I annotated my controller with @ConditionalOnProperty("ENABLE_API_COMPANY_SERVICE")
. If I store the property into applicaiton.properties
file then it works fine.
But whenever I try to read the property from external source like DB , it does not work. As per a different stack-overflow answer , the external property get loaded after the conditional property executed.
- I believe there should be some way in Spring Boot, this can be done using external property source like DB or Spring cloud Config. Anyone has any idea?
Now.... I tried using the @RefreshScope
annotation , by which one bean can be dynamically loaded without restarting the application. So I annotated my same controller with @RefreshScope
thinking that , if that controller can be re-initialized anytime after the application started, it should use the loaded property from external source. but this is also not working. Is this a stupid idea?
spring-mvc spring-boot
I am trying to dynamically enable/disable some service based on a flag, So I annotated my controller with @ConditionalOnProperty("ENABLE_API_COMPANY_SERVICE")
. If I store the property into applicaiton.properties
file then it works fine.
But whenever I try to read the property from external source like DB , it does not work. As per a different stack-overflow answer , the external property get loaded after the conditional property executed.
- I believe there should be some way in Spring Boot, this can be done using external property source like DB or Spring cloud Config. Anyone has any idea?
Now.... I tried using the @RefreshScope
annotation , by which one bean can be dynamically loaded without restarting the application. So I annotated my same controller with @RefreshScope
thinking that , if that controller can be re-initialized anytime after the application started, it should use the loaded property from external source. but this is also not working. Is this a stupid idea?
spring-mvc spring-boot
spring-mvc spring-boot
asked Nov 21 '18 at 18:27
MonajMonaj
2118
2118
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implementEnvironmentPostProcessor
orApplicationContextInitializer
?
– jbarrueta
Nov 21 '18 at 18:39
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
You need to add the properties very early an not use an@PropertySource
or worse (as you have) add an additionalPropertyPlaceholderConfigurer
(this might even break your application!). Instead use either anApplicationContextInitializer
or theEnvironmentPostProcessor
to add the DB drivenPropertySource
. That way it will be part of theEnvironment
as it should be.
– M. Deinum
Nov 21 '18 at 19:45
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04
add a comment |
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implementEnvironmentPostProcessor
orApplicationContextInitializer
?
– jbarrueta
Nov 21 '18 at 18:39
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
You need to add the properties very early an not use an@PropertySource
or worse (as you have) add an additionalPropertyPlaceholderConfigurer
(this might even break your application!). Instead use either anApplicationContextInitializer
or theEnvironmentPostProcessor
to add the DB drivenPropertySource
. That way it will be part of theEnvironment
as it should be.
– M. Deinum
Nov 21 '18 at 19:45
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implement
EnvironmentPostProcessor
or ApplicationContextInitializer
?– jbarrueta
Nov 21 '18 at 18:39
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implement
EnvironmentPostProcessor
or ApplicationContextInitializer
?– jbarrueta
Nov 21 '18 at 18:39
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
You need to add the properties very early an not use an
@PropertySource
or worse (as you have) add an additional PropertyPlaceholderConfigurer
(this might even break your application!). Instead use either an ApplicationContextInitializer
or the EnvironmentPostProcessor
to add the DB driven PropertySource
. That way it will be part of the Environment
as it should be.– M. Deinum
Nov 21 '18 at 19:45
You need to add the properties very early an not use an
@PropertySource
or worse (as you have) add an additional PropertyPlaceholderConfigurer
(this might even break your application!). Instead use either an ApplicationContextInitializer
or the EnvironmentPostProcessor
to add the DB driven PropertySource
. That way it will be part of the Environment
as it should be.– M. Deinum
Nov 21 '18 at 19:45
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04
add a comment |
0
active
oldest
votes
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
});
}
});
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%2f53418411%2fspring-boot-conditionalonproperty-does-not-work-on-external-property-source-exc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53418411%2fspring-boot-conditionalonproperty-does-not-work-on-external-property-source-exc%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
External sources work, for the case that you are reading the property from the DB, at what point are you adding the PropertySource to the Environment? Have you seen to implement
EnvironmentPostProcessor
orApplicationContextInitializer
?– jbarrueta
Nov 21 '18 at 18:39
@Jbarrueta, I have not seen those implementation , I will take a look on their uses. I just used "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" to load the properties.
– Monaj
Nov 21 '18 at 19:38
You need to add the properties very early an not use an
@PropertySource
or worse (as you have) add an additionalPropertyPlaceholderConfigurer
(this might even break your application!). Instead use either anApplicationContextInitializer
or theEnvironmentPostProcessor
to add the DB drivenPropertySource
. That way it will be part of theEnvironment
as it should be.– M. Deinum
Nov 21 '18 at 19:45
I will see both of those implementation and try to implement here..I will let you know the outcome.. Thanks
– Monaj
Nov 21 '18 at 20:04