Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring injects internal Beans


May 14, 2021 Spring


Table of contents


Inject internal Beans

As you know, Java inner classes are defined within the scope of other classes, and by the same to no other bean, inner beans are beans defined within the scope of other beans. Therefore, the element in the element is called the internal bean, as shown below.

<?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">

   <bean id="outerBean" class="...">
      <property name="target">
         <bean id="innerBean" class="..."/>
      </property>
   </bean>

</beans>

Example

We use the Eclipse IDE in the right place, and then follow these steps to create a Spring application:

Steps Describe
1 Create a project named SpringExample, and create a package com.tutorialspoint in the src folder that created the project.
2 Use the Add External JARs option to add the Spring library you want, as explained in the Spring Hello World Example section. option as explained in the chapter.
3 Create Java-like TextEditor, SpellChecker, and MainApp in the com.tutorialspoint package.
4 Create the Beans profile Beans file in the src .xml.
5 The final step is to create the contents of all Java files and bean profiles and run the application, as explained below.

Here is the contents of .java textEditor file:

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();
   }
}

Here's another class file you rely on, SpellChecker.java content:

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }   
}

Here's what .java mainApp files are:

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();
   }
}

Here is the profile Beans file that uses the internal bean for configuration based .xml injection:

<?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>

Once you've created the source code and bean profile, we're ready to run the application. If everything is fine with your application, the following information will be output:

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.