Spring - Injecting Inner Beans

As you know Java internal classes are characterized inside the extent of different classes, correspondingly, inner beans are beans that are characterized inside the extent of another bean. Accordingly, a <bean/> component inside the <property/> or <constructor-arg/> components is called inward bean and it is appeared/p>

<?xml adaptation = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/blueprint/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/blueprint/beans 


<bean id = "outerBean" class = "..."> 

<property name = "target"> 

<bean id = "innerBean" class = "..."/> 





Let us have working Eclipse IDE set up and pursue the accompanying strides to make a Spring application −

Steps Description
1 Create a venture with a name SpringExample and make a bundle com.tutorialspoint under the src envelope in the made project.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Create Java classes TextEditor, SpellChecker and MainApp under the com.tutorialspoint package.
4 Create Beans arrangement record Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java documents and Bean Configuration record and run the application as clarified below.

Here is the substance of TextEditor.java document −

package com.tutorialspoint; 

public class TextEditor { 

private SpellChecker spellChecker; 

//a setter strategy to infuse the dependency. 

public void setSpellChecker(SpellChecker spellChecker) { 

System.out.println("Inside setSpellChecker." ); 

this.spellChecker = spellChecker; 


//a getter strategy to return spellChecker 

public SpellChecker getSpellChecker() {

return spellChecker; 


public void spellCheck() { 




Following is the substance of another reliant class document SpellChecker.java

package com.tutorialspoint; 

public class SpellChecker { 

public SpellChecker(){ 

System.out.println("Inside SpellChecker constructor." ); 


public void checkSpelling(){ 

System.out.println("Inside checkSpelling." ); 



Following is the substance of the MainApp.java record −

package com.tutorialspoint;
import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ApplicationContext setting = new ClassPathXmlApplicationContext("Beans.xml"); 

TextEditor te = (TextEditor) context.getBean("textEditor"); 




Following is the design document Beans.xml which has setup for the setter-based infusion however utilizing inner beans

<?xml adaptation = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/construction/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/construction/beans 


<!- - Definition for textEditor bean utilizing inward bean - > 

<bean id = "textEditor" class = "com.tutorialspoint.TextEditor"> 

<property name = "spellChecker"> 

<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/> 




Once you are finished making the source and bean setup documents, let us run the application. On the off chance that all is well with your application, it will print the accompanying message −

Inside SpellChecker constructor. 

Inside setSpellChecker. 

Inside checkSpelling. 

© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3