I am trying to debug a verdor's app and it is giving me errors and exceptions at some points..
I would like to ask a conceptual question about auto wiring.
I have a class User that extends a class Profile that extends another class Operations that has an @AutoWired User user;
So its like this
Is it possible? a User has a property that has a property User. I know it is possible because it is a working code, but I think its recursively having the same object within itself. Should it not send me into a memory usage issue???
OR : When you Autowire a component you are actually referencing to it and it does not become part of the actual object ? I was under the impression that when you Autowire a component its actually a property of that class and Autowiring is only to populate it with respective values with whatever component its wired with. Am I wrong?
How can I prove my own assumptions or the other way?
And while I see no benefit of having an object reference itself, I don't see exactly why that couldn't happen. Unless it has to pass itself into its own constructor.
Autowired is an Annotation, when you tell Spring you have it and to do something about it, then Spring will go out and look at all of its beans it has and find one that is of type User. If it finds one then it injects it. With just @Autowired and it finds more than one, then it throws an exception because it autowires by type by default and if there is any ambiguity, you have to tell spring how to fix it.
Mark Spritzler wrote:One User could reference another User.
And while I see no benefit of having an object reference itself, I don't see exactly why that couldn't happen. Unless it has to pass itself into its own constructor.
Autowired is an Annotation, when you tell Spring you have it and to do something about it, then Spring will go out and look at all of its beans it has and find one that is of type User. If it finds one then it injects it. With just @Autowired and it finds more than one, then it throws an exception because it autowires by type by default and if there is any ambiguity, you have to tell spring how to fix it.
Mark
Thanks. well User is a session scoped component, which means there should be only one in session.
I thought this was a cyclic dependency: User has a property operations which has a property user which has a property operations which has a property user and so on..
Consider it without Spring and you are just instantiating the objects yourself and injection. Can you do with and not create too many objects. Also, if you ran it in Spring and it did kept making objects you would get a memory error, which it sounds like you aren't. But also think of it this way. I have 100 users logged in, so 100 Users created, it looks like the Operation is a singleton. So Spring will only create one and keep injecting the same one over and over. So Spring would create more than just that one, and in terms of User, Spring will only instantiate one User object per session and never create anymore. So you just have circular references, not a scenario that just keeps creating tons of new objects. So I don't think you will get more than 101 objects created in that scenario. So I take back my it depends, to no it won't, if I am correct, which I might not be. ;)
If your 'operation' bean is a singleton &
Your User object is in session scope.
1. You need to put an <aop:ScopedProxy> tag for the user configuration.
Without this tag it is unlikely the spring config will work correctly.
2. At startup - spring will create 1 object of Type Operation and I object Type User-Proxy (and not user).
The Operation - will be Autowired to this user-Proxy object.
At this point you don't have any user objects (since there are no sessions)
3. You web application starts being used and 100 users log in. Each login create a User object and all User objects get auto-wired to the same Operation object created in step 2.
4. When the Operation object refers to the User object - the User-Proxy returns the correct User object based on the current session
If your not familiar with scoped-proxies you should give it a read.
Google - scoped proxy