Is it possible to undeclare or define a null bean in Spring?











up vote
0
down vote

favorite












While using Spring, I've encountered a scenario in which some logic is called if a particular bean is not null. I do not want this logic to be called; therefore, I need this object to be null. The bean in question has a default non-null value created by autoconfiguration.



My question is this: is there a way to "undeclare" a bean so that it's null?



This won't work:



@Bean
public UserDetailsService userDetailsService() {
return null;
}


It yields:



org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDetailsService' is expected to be of type 'org.springframework.security.core.userdetails.UserDetailsService' but was actually of type 'org.springframework.beans.factory.support.NullBean'


Is there an Spring configuration way to do this or do I have to dive in and call constructors and setters to set this object to null?










share|improve this question






















  • Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
    – Eamon Scullion
    Nov 9 at 0:04










  • Have you tried to cast null to a type? return (UserDetailsService) null;
    – uli
    Nov 9 at 0:08










  • Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
    – Darren Forsythe
    Nov 9 at 0:18















up vote
0
down vote

favorite












While using Spring, I've encountered a scenario in which some logic is called if a particular bean is not null. I do not want this logic to be called; therefore, I need this object to be null. The bean in question has a default non-null value created by autoconfiguration.



My question is this: is there a way to "undeclare" a bean so that it's null?



This won't work:



@Bean
public UserDetailsService userDetailsService() {
return null;
}


It yields:



org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDetailsService' is expected to be of type 'org.springframework.security.core.userdetails.UserDetailsService' but was actually of type 'org.springframework.beans.factory.support.NullBean'


Is there an Spring configuration way to do this or do I have to dive in and call constructors and setters to set this object to null?










share|improve this question






















  • Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
    – Eamon Scullion
    Nov 9 at 0:04










  • Have you tried to cast null to a type? return (UserDetailsService) null;
    – uli
    Nov 9 at 0:08










  • Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
    – Darren Forsythe
    Nov 9 at 0:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











While using Spring, I've encountered a scenario in which some logic is called if a particular bean is not null. I do not want this logic to be called; therefore, I need this object to be null. The bean in question has a default non-null value created by autoconfiguration.



My question is this: is there a way to "undeclare" a bean so that it's null?



This won't work:



@Bean
public UserDetailsService userDetailsService() {
return null;
}


It yields:



org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDetailsService' is expected to be of type 'org.springframework.security.core.userdetails.UserDetailsService' but was actually of type 'org.springframework.beans.factory.support.NullBean'


Is there an Spring configuration way to do this or do I have to dive in and call constructors and setters to set this object to null?










share|improve this question













While using Spring, I've encountered a scenario in which some logic is called if a particular bean is not null. I do not want this logic to be called; therefore, I need this object to be null. The bean in question has a default non-null value created by autoconfiguration.



My question is this: is there a way to "undeclare" a bean so that it's null?



This won't work:



@Bean
public UserDetailsService userDetailsService() {
return null;
}


It yields:



org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDetailsService' is expected to be of type 'org.springframework.security.core.userdetails.UserDetailsService' but was actually of type 'org.springframework.beans.factory.support.NullBean'


Is there an Spring configuration way to do this or do I have to dive in and call constructors and setters to set this object to null?







java spring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 0:00









Mark S

1,3101225




1,3101225












  • Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
    – Eamon Scullion
    Nov 9 at 0:04










  • Have you tried to cast null to a type? return (UserDetailsService) null;
    – uli
    Nov 9 at 0:08










  • Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
    – Darren Forsythe
    Nov 9 at 0:18


















  • Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
    – Eamon Scullion
    Nov 9 at 0:04










  • Have you tried to cast null to a type? return (UserDetailsService) null;
    – uli
    Nov 9 at 0:08










  • Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
    – Darren Forsythe
    Nov 9 at 0:18
















Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
– Eamon Scullion
Nov 9 at 0:04




Is it safe to assume that you are using an annotation (e.g @Service) for UserDetailsService class definition?
– Eamon Scullion
Nov 9 at 0:04












Have you tried to cast null to a type? return (UserDetailsService) null;
– uli
Nov 9 at 0:08




Have you tried to cast null to a type? return (UserDetailsService) null;
– uli
Nov 9 at 0:08












Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
– Darren Forsythe
Nov 9 at 0:18




Why declare it in the first place? Realistically the answer is no there isn't. Maybe more details on the issue you have with defining the bean and why you do not need it will help give a concise answer to your issue
– Darren Forsythe
Nov 9 at 0:18












1 Answer
1






active

oldest

votes

















up vote
1
down vote













It sounds like you do not want UserDetailsService to be treated as a bean, therefore you shouldn't create it as one. Remove annotations setting it up as a bean (e.g @Service, @Bean)






share|improve this answer























  • This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
    – Mark S
    Nov 9 at 0:34











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',
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%2f53217939%2fis-it-possible-to-undeclare-or-define-a-null-bean-in-spring%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













It sounds like you do not want UserDetailsService to be treated as a bean, therefore you shouldn't create it as one. Remove annotations setting it up as a bean (e.g @Service, @Bean)






share|improve this answer























  • This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
    – Mark S
    Nov 9 at 0:34















up vote
1
down vote













It sounds like you do not want UserDetailsService to be treated as a bean, therefore you shouldn't create it as one. Remove annotations setting it up as a bean (e.g @Service, @Bean)






share|improve this answer























  • This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
    – Mark S
    Nov 9 at 0:34













up vote
1
down vote










up vote
1
down vote









It sounds like you do not want UserDetailsService to be treated as a bean, therefore you shouldn't create it as one. Remove annotations setting it up as a bean (e.g @Service, @Bean)






share|improve this answer














It sounds like you do not want UserDetailsService to be treated as a bean, therefore you shouldn't create it as one. Remove annotations setting it up as a bean (e.g @Service, @Bean)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 9:00

























answered Nov 9 at 0:14









Eamon Scullion

683313




683313












  • This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
    – Mark S
    Nov 9 at 0:34


















  • This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
    – Mark S
    Nov 9 at 0:34
















This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
– Mark S
Nov 9 at 0:34




This does not solve the problem. Removing the @Bean annotation from the UserDetailsService declaration causes spring to inject a bean of type InMemoryUserDetailsService, which is the default. I do not want this behavior; I want the userDetailsService field on this object to be null.
– Mark S
Nov 9 at 0:34


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217939%2fis-it-possible-to-undeclare-or-define-a-null-bean-in-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

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff