Results 1 to 5 of 5

Thread: Inner bean

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

    Default Inner bean

    What is the difference between a bean being dependent on another bean and a bean having an inner bean?

    I mean, is there a difference in the first place?

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: Inner bean

    Give a code example of what you're talking about.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Inner bean

    Inner Bean Example & Dependent Bean Example:

    XML for inner bean example:
    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 using inner bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <property name="spellChecker">
             <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
           </property>
       </bean>
    
    </beans>
    XML for dependent bean example:
    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>
    Common Files(Inner bean Example & Dependent Bean Example)
    TextEditor.java
    Java Code:
    package com.tutorialspoint;
    
    public class TextEditor {
       private SpellChecker spellChecker;
    
       // a setter method to inject the dependency.
       public void setSpellChecker(SpellChecker spellChecker) {
          System.out.println("Inside setSpellChecker." );
          this.spellChecker = spellChecker;
       }
       // a getter method to return spellChecker
       public SpellChecker getSpellChecker() {
          return spellChecker;
       }
    
       public void spellCheck() {
          spellChecker.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();
       }
    }
    So what's the difference or is there no difference at all?

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

    Default Re: Inner bean

    argh that same Spring stuff again. When you are you going to drop this and read the manual?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Inner bean

    Register two TextEditor objects in your xml file, textEditor1 and textEditor2 in each of your test environments.
    Load them in your main and see what happens when you check whether the spellChecker in each is the same:
    Java Code:
          TextEditor te1 = (TextEditor) context.getBean("textEditor1"); 
          TextEditor te2 = (TextEditor) context.getBean("textEditor2");
        System.out.println("spell checker the same " + te1.getSpellChecker() == te2.getSpellChecker");
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 07:55 AM
  2. any way to communicate one bean to another bean not using session in jsf
    By MayaKannan in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 01-14-2011, 01:45 PM
  3. UREGENT-accessing session bean ok but entity bean
    By parimal in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-28-2008, 01:34 PM
  4. Replies: 0
    Last Post: 03-30-2008, 10:03 AM
  5. Replies: 0
    Last Post: 09-26-2007, 08:36 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
  •