IntelliJ Java IDE
The moose likes Spring and the fly likes @Autowired component within it self Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Spring
Reply Bookmark "@Autowired component within it self" Watch "@Autowired component within it self" New topic
Author

@Autowired component within it self

Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 44
Hi All!

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?

thanks
Syed...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 44
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..

Am I correct ?

will it not consume a lot of mem usage

thanks and appreciate the reply.

Syed..
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

It depends on the configuration.

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. ;)

Mark
Sam Mercs
Ranch Hand

Joined: Feb 08, 2009
Posts: 539

Agree with what Mark say's here.

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

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 44
such nice and detailed explanations.

thank you sooo much.. both of you.

I really appreciate the answers.

confusion cleared.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

Syed Mahdi wrote:such nice and detailed explanations.

thank you sooo much.. both of you.

I really appreciate the answers.

confusion cleared.


Fantastic, that is why we are here and are much better than some site like Stack Overflow.

Mark
 
 
subject: @Autowired component within it self
 
Threads others viewed
Hard time getting Spring working with WS'.
Spring And Hibernate (LocalSessionFactoryBean)
Error: Bean property 'userDAO' is not writable or has an invalid setter method.
how to inject DAO with Spring???
@Autowired, @Qualifier and @Resource annotation
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture