Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: How does really work bean initialization???

  1. #11
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    I guess the easiest way to solve your problem is to switch from setter injection to constructor parameter injection for PropertyManager in the PositionBatch class.

    Regards,
    Oleksandr

  2. #12
    Join Date
    Aug 2007
    Posts
    8

    Question Bean Creation and Initialization

    Hi,

    I know that setters are set after Bean Creation, but during Bean Creation, what happens to properties of the class?

    Suppose there is boolean var - will it be set to false?
    and what about any Map variable - set to null / initialized (by reading from database )?
    (Note : by default, maps are lazily loaded)

    And, is order correct - 1) Bean Creation 2) Setters are called 3) Bean Initialization
    If not, what is the correct order?

    Actually, I want to instantiate a Map property of a class depending on the value of a boolean variable of the same class after setting it from applicationContext.xml.
    How to do it?

    Thanks,
    Sucheta.

  3. #13
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    There is not Spring-specific in this case. It behaves like any other Java application. First object is created with appropriate constructor (even if object is created by factory method somewhere in that method constructor shall be called!) and all object fields are initialized by JVM according to Java Language Specification.
    It means that all fields of non-primitive that have no explicit initializers and are not initialized by constructor are set to null, numeric fields to 0, boolean to false etc.

    And only then Spring sets properties, apply post methods etc.

    Regards,
    Oleksandr

    Quote Originally Posted by Sucheta24 View Post
    Hi,

    I know that setters are set after Bean Creation, but during Bean Creation, what happens to properties of the class?

    Suppose there is boolean var - will it be set to false?
    and what about any Map variable - set to null / initialized (by reading from database )?
    (Note : by default, maps are lazily loaded)

    And, is order correct - 1) Bean Creation 2) Setters are called 3) Bean Initialization
    If not, what is the correct order?

    Actually, I want to instantiate a Map property of a class depending on the value of a boolean variable of the same class after setting it from applicationContext.xml.
    How to do it?

    Thanks,
    Sucheta.

  4. #14
    Join Date
    Aug 2007
    Posts
    8

    Question Continued...

    Hi ,

    Thanks for your reply.

    But then as I checked in log - getValues method being called in AbstractEntityPersister by Dealers.java (another class). Whereas setter being called after that.
    And there (in AbstractEntityPersister) map object being accessed assuming the value of boolean variable to be false.

    I am attaching the code(LocalizedString.java) to make the point clear.

    Thanks,
    Sucheta.

  5. #15
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Sorry, your attacment seems to be lost in space ...

    Quote Originally Posted by Sucheta24 View Post
    Hi ,

    Thanks for your reply.

    But then as I checked in log - getValues method being called in AbstractEntityPersister by Dealers.java (another class). Whereas setter being called after that.
    And there (in AbstractEntityPersister) map object being accessed assuming the value of boolean variable to be false.

    I am attaching the code(LocalizedString.java) to make the point clear.

    Thanks,
    Sucheta.

  6. #16
    Join Date
    Aug 2007
    Posts
    8

    Default hey...

    I tried that one - from setters to constructors, its not helping.

    And, sorry i was unable to attach any docs , guess i haven't been given the permission.

    Thanks,
    Sucheta.

  7. #17
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Hello,

    concerning attachments - take a look on an info just above "Manage attachments" button.
    Code:
    Valid file extensions: bmp doc gif jpe jpeg jpg pdf png psd txt zip
    So if you try to attach any java/xml code you have to zip them.

    Regards,
    Oleksandr

  8. #18
    Join Date
    Aug 2007
    Posts
    8

    Default Initialization of Map using Hibernate Annotation

    Hey,

    First of all, I am really very thankfull to you for your quick replies.

    My ques is even when I use Hibernate Annotations, is then also Map variable get initialized to null or with values from databse ?

    And when objects get persisted in database (when there is no further refernce to object or as soon as any fields are changed in the object)?

    Thanks,
    Sucheta.

    (I have attached the code, I checked logs and conclusion is setters are called after all objects get created that are being referenced in SessionFactory.)
    Attached Files Attached Files

  9. #19
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Hello,

    Hibernate annotations do not influence a way how object fields are initialized when object is created with new operator.
    They only establish mapping to database (and some related issues). Till you load object from DB or save it to DB they do not come into play. And anyway, first Hibernate constructs object with default (i.e. no-arg) constructor, by this construction all fields are set to their defaults according to Java Language Specification and only then Hibernate set fields to values acqured from DB.

    And when objects are persisted - when you persist them i.e. when you flush/commit session (directly or indirectly, by means of Spring Hibernate template or declarative transaction mangement).

    More exactly, if objects are attached to the session they are saved by flush()/commit() and if you work with detached objects you need to persist them explicitly (e.g. saveOrUpdate) before flush/commit. There are to many details about this process that just can not be covered in a post - their explanation requires if not the whole book, then at least 1-2 chapters. And my strong suggestion is to read carefully one of the good books about Hibernate, preferably, "Java Persistence with Hibernate. Second Edition of Hibernate in Action" by Christian Bauer and Gavin King (latter is main developer of Hibernate AFAIK).

    BTW, I really do not see how code attached to your post relates to the problem from which this thread has started.

    Regards,
    Oleksandr

  10. #20
    Join Date
    Aug 2007
    Posts
    8

    Default

    Hey,

    See, I want Map to be loaded with values from database only when dealersite (boolean variable) is false else not.
    Is there any way to do it? (will the current code do that? )

    Thanks,
    Sucheta.

Posting Permissions

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