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?
java spring
add a comment |
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?
java spring
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
add a comment |
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?
java spring
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
java spring
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
add a comment |
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
add a comment |
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)
This does not solve the problem. Removing the@Bean
annotation from theUserDetailsService
declaration causes spring to inject a bean of typeInMemoryUserDetailsService
, 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
add a comment |
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)
This does not solve the problem. Removing the@Bean
annotation from theUserDetailsService
declaration causes spring to inject a bean of typeInMemoryUserDetailsService
, 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
add a comment |
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)
This does not solve the problem. Removing the@Bean
annotation from theUserDetailsService
declaration causes spring to inject a bean of typeInMemoryUserDetailsService
, 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
add a comment |
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)
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)
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 theUserDetailsService
declaration causes spring to inject a bean of typeInMemoryUserDetailsService
, 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
add a comment |
This does not solve the problem. Removing the@Bean
annotation from theUserDetailsService
declaration causes spring to inject a bean of typeInMemoryUserDetailsService
, 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
add a comment |
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%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
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
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