• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to read xml file and inject bean reference using Spring Framework?

July 5, 2008 //  by Krishna Srinivasan//  Leave a Comment

Introduction

This article presents sample source code for creating the Bean instance using the Spring Framework. This is basic example to read configuration file from the file system and create the beans from the applicationContext.xml file. This xml file define two beans Employee and Address. Address bean is injected inside Employee bean using the ref attribute. If you are looking for more spring frameworkrelated resources, please read our list of Spring framework articles.

also read:

  • Spring Tutorials
  • Spring 4 Tutorials
  • Spring Interview Questions

Sample Source Code

Employee.java

package javabeat.spring;
/**
 * Source : https://javabeat.net
 */

public class Employee {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Address.java

package javabeat.spring;
/**
 * Source : https://javabeat.net
 */

public class Address {
    public Address(){

    }
    private String street;
    private String city;
    private String pincode;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

MainClass.java

package javabeat.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
 * Source : https://javabeat.net
 */
public class MainClass {
    public static void main(String args[]) {
        Resource xmlResource = new FileSystemResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(xmlResource);
        Employee employeeBean = (Employee)factory.getBean("employeeBean");
        System.out.println(employeeBean.getName());
        System.out.println(employeeBean.getAddress().getStreet());
        System.out.println(employeeBean.getAddress().getCity());
        System.out.println(employeeBean.getAddress().getPincode());
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="addressBean" class="javabeat.spring.Address">
        <property name="street">
            <value>Street</value>
        </property>
        <property name="city">
            <value>Bangalore</value>
        </property>
        <property name="pincode">
            <value>567456</value>
        </property>
    </bean>
    <bean id="employeeBean" class="javabeat.spring.Employee">
        <property name="name">
            <value>Krishna Srinivasan</value>
        </property>
        <property name="address">
            <ref bean="addressBean"/>
        </property>
    </bean>
</beans>

also read:

  • Spring Books
  • Introduction to Spring Framework
  • Introduction to Spring MVC Framework

Category: Spring FrameworkTag: Spring IOC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to implement ActionListener (f:actionListener) in JSF?
Next Post: How to implement f:phaseListener for h:commandButton? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact