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

    Default Flow of Basic example

    How does the SpellChecker constructor called before Static block mentioned in TextEditor.java? Somebody pls. explain the whole flow


    Starting from the output:
    Output:
    Inside SpellChecker constructor.
    Hello
    Inside TextEditor constructor.
    1
    2
    Inside checkSpelling.


    TextEditor.java
    Java Code:
    package com.tutorialspoint;
    
    public class TextEditor {
       private SpellChecker sspellChecker;
       static
       {
    	   System.out.println("Hello");
       }
       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();
       }
    }
    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." );
       }
       
    }
    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();
       }
    }
    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>

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Flow of Basic example

    This is just a guess, but probably because Spring uses reflection to create the classes? You could put together a test program that uses reflection without Spring to see what happens. You could also print some stack traces along with each print statement for more information. But these are just guesses, so I'd be curious what you come up with.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Flow of Basic example

    Might also simply be how the JVM loads its classes, without any link specifically to reflection. I can imagine it won't be necessary to load the class until the first instance of such a class is actually created, and Spring is forced to construct the SpellChecker bean instance first because it is a parameter to the constructor according to the beans.xml.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Flow of Basic example

    Is this the concept of inversion of control where the dependent objects are first created and kept ready so that they can be used by classes which utilize them?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Flow of Basic example

    Well basically yes, but there is more to it than that. The Spring manual explains it all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Flow of Basic example

    From the beans.xml Spring knows that the TextEditor object you want to create requires the spellChecker.
    (This is all done during the parsing of the file).
    So Spring knows it has to create spellChecker first.

    So the SpellChecker class is loaded.

    Then Spring can create the TextEditor...which is the point at which the TextEditor is loaded (the first time it is referenced).
    Please do not ask for code as refusal often offends.

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

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2013, 09:30 PM
  2. Flow layout
    By Shahnawaz in forum New To Java
    Replies: 2
    Last Post: 10-04-2011, 09:24 PM
  3. Flow-Charting?
    By danthegreat in forum Forum Lobby
    Replies: 4
    Last Post: 09-12-2011, 06:24 AM
  4. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2010, 11:21 PM
  5. program flow
    By loktamu in forum AWT / Swing
    Replies: 1
    Last Post: 09-17-2008, 04:52 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
  •