Spiring bean scope related query
okay so i was just making a mini project where the first page is a login page and when you login successfully youll get a page where you can add items to your cart and also option to display your cart
i made a session mapped LoginService class which has a private User attribute
so whenever there was a succesful login i was assigning the user attribute of LoginService an object(the first function)
and whenever an item was to be added i was adding it to the user attribute of LoginService bean(second method)
but different LoginService beans are injected into the first and second method. Why is that so. isnt LoginService SessionMapped??
31 Replies
⌛
This post has been reserved for your question.
Hey @lucifer7303! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
going by the console log the methods have different beans injected into them
tldr:different session scoped beans being injected even tho same session
Because you can't inject a bean as an argument. But Spring will helpfully try to create a new instance of the class at each invocation of the handler.
@Qualifier
is no-op here.Isnt dependancy injection a thing?
It is injecting a bean anyway, just different beans
I fixed it by making loginService aa field kf UserController class and autowiring it throught constructor but still dont know why the previous method didnt work
You can inspect Spring internals if you're courageous enough.
But, just like I've said. It doesn't create a new bean, it only constructs a new instance of the class.
so is this the internal code of @Qualifier or @SessionScope
if i leave the LoginService parameter as is, itll get the same bean im guessing?
Neither. Annotations aren't decorators, they don't comprise any logic. Yes, that's terrible and leads to mysterious bugs.
by as is i mean, without @Qualifier
@Qualifier
in the original code is simply ignored.okay
so how am i supposed to get the same bean of LoginService
when asking for it in a a method
Just inject it to the controller.
i did eventually went around it by making it a field of Controller
Ay
but why is it even creating a new bean if it is sesssio scoped
shouldnt it return the one it already has in the context
Again, technically it's not a bean in this context.
and what if for some unknown reason i do want a consistent bean in a method without making that said class an attribute
dont really get what you mean by that?
really new to Spring to bear with me 😬
Bean is an instance of class which lifetime is controlled by a Spring context.
true true
so when spring is injeccting it into the method , isnt it creating a bean of that class
In this code Spring creates a new object, but it isn't a bean.
or just injecting random ass object
so why does an AutoWired constructor work but not this
And there's really no reason to inject beans into a single method.
arent both using the DI functionality of IOC principle
If you really want to do that for some weird reason you can inject the context into your controller and access required beans manually.
??
ig AutoWired tells spring to make a bean
No, it's a different mechanism https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/arguments.html
but just DI injects a random object
so if i were to manually add a bean throught addBean or a Configuration class
the same bean will be injected into the method
Nope
If a method argument is not matched to any of the earlier values in this table and it is a simple type (as determined by BeanUtils#isSimpleProperty), it is resolved as a @RequestParam. Otherwise, it is resolved as a @ModelAttribute.
but now the bean is in the context right?
so it should inject that bean if im interpreting this code right
oh okay there is a @SessionAttribute annot for this
for this use case
if and when required
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.