Results 1 to 17 of 17
  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Static block execution order diff. for setter-based and constructor-based injection

    If we compare the below outputs, the static block(Hello) executes as the first statement in setter-based injection while the static block(Hello) is the second statement in the output in the constructor based injection. Why is it like that?

    Setter based injection output:
    Hello
    Inside SpellChecker constructor.
    Inside setSpellChecker.
    1
    2
    Inside checkSpelling.

    Constructor based injection output:
    Inside SpellChecker constructor.
    Hello
    Inside TextEditor constructor.
    1
    2
    Inside checkSpelling.

    Will post the code in the next post.
    Last edited by suhaas.mohandos@gmail.com; 08-08-2013 at 07:33 AM.

  2. #2
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Setter based injection:

    MainApp.java
    Java Code:
    package com.tutorialspoint;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
    
          TextEditor te = (TextEditor) context.getBean("textEditor");
    
          te.spellCheck();
       }
    }
    SpellChecker.java
    Java Code:
    package com.tutorialspoint;
    
    public class SpellChecker {
       public SpellChecker(){
          System.out.println("Inside SpellChecker constructor." );
       }
    
       public void checkSpelling() {
          System.out.println("Inside checkSpelling." );
       }
       
    }
    TextEditor.java
    Java Code:
    package com.tutorialspoint;
    
    public class TextEditor {
    	   static
    	   {
    		   System.out.println("Hello");
    	   }
       private SpellChecker spellChecker;
    
       // a setter method to inject the dependency.
       public void setSpellChecker(SpellChecker spellChecker) {
          System.out.println("Inside setSpellChecker." );
          System.out.println("1");
          this.spellChecker = spellChecker;
          System.out.println("2");
       }
       // a getter method to return spellChecker
       public SpellChecker getSpellChecker() {
          return spellChecker;
       }
    
       public void spellCheck() {
          spellChecker.checkSpelling();
       }
    }
    Beans.xml
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <property name="spellChecker" ref="spellChecker"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>
    Constructor based injection:
    MainApp.java
    Java Code:
    package com.tutorialspoint;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          TextEditor te = (TextEditor) context.getBean("textEditor");
    
          te.spellCheck();
       }
    }
    SpellChecker.java
    Java Code:
    package com.tutorialspoint;
    
    public class SpellChecker {
       public SpellChecker(){
          System.out.println("Inside SpellChecker constructor." );
       }
    
       public void checkSpelling() {
          System.out.println("Inside checkSpelling." );
       }
       
    }
    TextEditor.java
    Java Code:
    package com.tutorialspoint;
    
    public class TextEditor {
    	   static
    	   {
    		   System.out.println("Hello");
    	   }
    	private SpellChecker sspellChecker;
    
       public TextEditor(SpellChecker spellChecker) {
          System.out.println("Inside TextEditor constructor." );
          System.out.println("1");
          sspellChecker = spellChecker;
          System.out.println("2");
       }
       public void spellCheck() {
          sspellChecker.checkSpelling();
       }
    }
    Beans.xml
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <constructor-arg ref="spellChecker"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Did you have a question?

    db

    edit: Oh, I see the question is in the first post.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    This is a dupe question really. The same people are going to give the same answers as in your last thread you know.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Quote Originally Posted by gimbal2 View Post
    This is a dupe question really. The same people are going to give the same answers as in your last thread you know.
    Quite.
    You really need to explain what you think is happening, otherwise people might think you;re getting them to answer some homework questions.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    No, the question I asked was based on setter-based injection. But I realised the static block execution order is different for setter-based injection and constructor based injection. Thats why I created a new thread.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    But it's the same logic.
    You can even read up on it in the Spring documentation (as suggested, I believe, by gimbal2 in the other thread).

    So try and tell us what you think is happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Static block executes as the first statement for setter-based injection and executes as the second statement for the constructor based injection. I don't know what is happening. So how will I tell you what I think is happening?

    I look at the code examples and try to play around with the code. In the process, I get some questions, so I post in forum. What is wrong with that? I cannot find the exact answer to my questions in tutorials, hence I post them. Tell me if I am doing something wrong.

    No offence intended.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Do you know when (in normal code) a static block is executed in Java?

    The fact that you cannot find the answer in tutorials is down to the fact that these answers are probably in the documentation.

    Try and think through how Spring parses that bean.xml, if digging out the docs is too much hard work.
    But, remember, without actual documentation it will be just a guess and the actual answer might be gimbal2's...it could simply be random.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Static blocks are executed before main() method. How can the static block execution block be different for setter-based injection and constructor-based injection? Or, are you sure that it's random? Somebody pls confirm.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Static blocks are executed before main() method.
    And this is why I wanted you to explain your thinking on this.
    It helps us to see where exactly you have got confused.

    The JVM only loads a class when it is first accessed. It does not load everything up at start up.
    So the TextBook class is only loaded when Spring attempts to create the object, which happens here:
    Java Code:
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    The order in which the classes are loaded is probably (and this is simply a guess on my part, and I doubt Spring even bothers to guarantee an order of loading, hence gimbal2 saying it could well be random) simply the order they appear in the xml file if there are no constructor-based dependencies.

    So for constructor-based, the SpellChecker is loaded first as it is needed to build the TextBook.
    For setter-based the TextBook is loaded first as it appears first in the xml.
    You could, in fact, easily test that assumption by changing the order in which the classes appear in the xml file.

    ETA: Note, it would undoubtedly be a Bad Thing to rely on any sort of ordering of class-loading with Spring. If something is dependent on something else being loaded first then make it proper dependency and inject it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Quote Originally Posted by Tolls View Post
    The order in which the classes are loaded is probably (and this is simply a guess on my part, and I doubt Spring even bothers to guarantee an order of loading, hence gimbal2 saying it could well be random)
    Oi, that was another thread. I said that about the ordering of properties fetched from the Properties class :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Oh dear, all these threads are blending into one!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    "I don't want to read so I repeatedly ask the same questions in a slightly different form" threads tend to do that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    The JVM only loads a class when it is first accessed.
    Ok

    So for constructor-based, the SpellChecker is loaded first as it is needed to build the TextBook.
    For setter-based the TextBook is loaded first as it appears first in the xml.


    Both(setter injection, constructor injection) require an instance of the SpellChecker class for it to execute. So how can the static block execution be different.

    You could, in fact, easily test that assumption by changing the order in which the classes appear in the xml file.
    Ya, I checked that.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    The JVM only loads a class when it is first accessed.
    Ok

    So for constructor-based, the SpellChecker is loaded first as it is needed to build the TextBook.
    For setter-based the TextBook is loaded first as it appears first in the xml.


    Both(setter injection, constructor injection) require an instance of the SpellChecker class for it to execute. So how can the static block execution be different.
    Yes, but not both require the SpellChecker to be available when the TextEditor is constructed.
    In the constructor version you *have* to have a SpellChecker in order to create a TextEditor.
    In the setter version you can create a TextEditor (and this is how Spring does it) and later assign the SpellChecker using the setter.

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    You could, in fact, easily test that assumption by changing the order in which the classes appear in the xml file.
    Ya, I checked that.
    And? Was the order different with the setter?
    Just for my own curiosity more than anything else.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Static block execution order diff. for setter-based and constructor-based injecti

    Yes, but not both require the SpellChecker to be available when the TextEditor is constructed.
    In the constructor version you *have* to have a SpellChecker in order to create a TextEditor.
    In the setter version you can create a TextEditor (and this is how Spring does it) and later assign the SpellChecker using the setter.


    Appreciate it man.

Similar Threads

  1. [SOLVED] static block or constructor?
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 05-27-2008, 02:21 PM
  2. Replies: 0
    Last Post: 03-29-2008, 12:38 PM
  3. Replies: 0
    Last Post: 03-29-2008, 12:38 PM
  4. Replies: 0
    Last Post: 09-26-2007, 08:29 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:28 PM

Posting Permissions

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