Blog Image

Example On Inner Bean  

In this blog we are going to discuss about Inner beans while working with Configuration-Approach of Spring.

To manage dependencies between the classes in Spring we need to declare the classes as Bean Definitions in Spring Bean Configuration File and should inject them via <property/> (or) <constructor-arg/> as reference.

In case of Inner beans, we declare the dependent bean inside the <property/> (or) <constructor-arg/> of target bean as shown below.

Let us See the example on Inner Beans and how we can use in our projects lets explore.

Technologies used :-
 1) java (1.8).
 2) maven (3.5.2).
 3) Spring (4.0.0).

Project Structure

Execute below command for creating maven project.

mvn archetype:generate -DgroupId=com.sjg

Below is the maven project structure with packages and java src files.


package com.sjg.beans;

public class BiCycle {
    private Chain chain;

    public void setChain(Chain chain) {
        this.chain = chain;

    public String toString() {
        return "BiCycle [chain=" + chain + "]";


package com.sjg.beans;

public class Chain {
    private int id;
    private String manufacturer;

    public void setId(int id) { = id;

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;

    public String toString() {
        return "Chain [id=" + id + ", manufacturer=" + manufacturer + "]";



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="";

    <bean id="biCycle" class="com.sjg.beans.BiCycle">
        <property name="chain">
            <bean class="com.sjg.beans.Chain">
                <property name="id" value="19" />
                <property name="manufacturer" value="atlas" />


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

<project xmlns=""; xmlns:xsi="";


    <!-- FIXME change it to the project's website -->


package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import com.sjg.beans.BiCycle;

public class Test {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        BiCycle biCycle = factory.getBean("biCycle", BiCycle.class);


Please find attached the as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.