Results 1 to 5 of 5

Thread: @Autowired a component with in itself ?

  1. #1
    Join Date
    Nov 2011
    Posts
    14

    Default @Autowired a component with in itself ?

    Hi All!

    I am trying to debug a verdor's app and it is giving me errors and exceptions at some points..
    (This questions a bit different from the previous one i asked so I am making a new topic so as to not confuse)

    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

    Code:
    @Component("user")
    @Scope("session")
    public class User extends Profile
    {
    
    }
    //No @Component or any annotation
    public class Profile 
    {
    
           @Autowired
    	private Operations operations;
    }
    
    @Component("operations")
    public class Operations
    {
    
            @Autowired
    	private User user;
    
           @Autowired
           private LoginDao logindao
    
    }

    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 object 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...

  2. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    In the code you've shown here, the "Profile" class does not extend the "Operations" class.

  3. #3
    Join Date
    Nov 2011
    Posts
    14

    Default

    Thanks for the reply

    the profile doesnt extend operations but operations is a field in the class that has the User class child of parent.

    The structure is the same as I shown: dont you think there should be a problem any where.

    Can i not say it that User class is having a property operations that has a User as a property as well.

    it doesnt make sense but dont you think its recursively having an object of self within it self. seems like a circular dependency to me but im not an expert here esp with annotations and DI.

    Thanks again for your reply, will appreciate some pointers to understanding this.

    Syed...

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    When the Spring container starts up, it instantiates all the beans in the "first step". In the "second step", it assigns property values. When the second steps starts, both the "user" and "operations" beans exist. The "user.operations" property gets set to the "operations" bean, and the "operations.user" property gets set to the "user" bean. There is nothing inherently fatal here.

  5. #5
    Join Date
    Nov 2011
    Posts
    14

    Default

    ok I understand that step but when they both have the references to each other, from a memory point of view is it not going to become heavy?

    operations.user property is set to have operations bean which will have a user bean which will have operations bean and so on....

    wont that consume a lot of memory. It seemed to be recursive to me.

    Thanks for taking the time and pls do continue if you can pls.
    Appreciate your answer and your future ones.

    thanks.
    Syed..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •