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
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @lucifer7303! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
lucifer7303
lucifer7303OP6mo ago
No description
No description
lucifer7303
lucifer7303OP6mo ago
going by the console log the methods have different beans injected into them tldr:different session scoped beans being injected even tho same session
szatkus
szatkus6mo ago
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.
lucifer7303
lucifer7303OP6mo ago
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
szatkus
szatkus6mo ago
You can inspect Spring internals if you're courageous enough.
No description
szatkus
szatkus6mo ago
But, just like I've said. It doesn't create a new bean, it only constructs a new instance of the class.
lucifer7303
lucifer7303OP6mo ago
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?
szatkus
szatkus6mo ago
Neither. Annotations aren't decorators, they don't comprise any logic. Yes, that's terrible and leads to mysterious bugs.
lucifer7303
lucifer7303OP6mo ago
by as is i mean, without @Qualifier
szatkus
szatkus6mo ago
@Qualifier in the original code is simply ignored.
lucifer7303
lucifer7303OP6mo ago
okay so how am i supposed to get the same bean of LoginService when asking for it in a a method
szatkus
szatkus6mo ago
Just inject it to the controller.
lucifer7303
lucifer7303OP6mo ago
i did eventually went around it by making it a field of Controller
szatkus
szatkus6mo ago
Ay
lucifer7303
lucifer7303OP6mo ago
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
szatkus
szatkus6mo ago
Again, technically it's not a bean in this context.
lucifer7303
lucifer7303OP6mo ago
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 😬
szatkus
szatkus6mo ago
Bean is an instance of class which lifetime is controlled by a Spring context.
lucifer7303
lucifer7303OP6mo ago
true true so when spring is injeccting it into the method , isnt it creating a bean of that class
szatkus
szatkus6mo ago
In this code Spring creates a new object, but it isn't a bean.
lucifer7303
lucifer7303OP6mo ago
or just injecting random ass object so why does an AutoWired constructor work but not this
szatkus
szatkus6mo ago
And there's really no reason to inject beans into a single method.
lucifer7303
lucifer7303OP6mo ago
arent both using the DI functionality of IOC principle
szatkus
szatkus6mo ago
If you really want to do that for some weird reason you can inject the context into your controller and access required beans manually.
lucifer7303
lucifer7303OP6mo ago
?? ig AutoWired tells spring to make a bean
lucifer7303
lucifer7303OP6mo ago
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
szatkus
szatkus6mo ago

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.
Nope
lucifer7303
lucifer7303OP6mo ago
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
JavaBot
JavaBot6mo ago
💤 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.
Want results from more Discord servers?
Add your server