Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Calling Constructor in Spring

In the given example you will be learning about a constructor and how to call a constructor in the Spring.


Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration

Calling Constructor in Spring


In the given example you will be learning about a constructor and how to call a constructor in the Spring. Declaring constructor injection in the Spring framework is generally done in the bean section of the configuration file that is the context.xml file. 

Steps to declare Constructor Injection:
1)Set the constructor declaration
2)Pass the value to the constructor.
This tutorial describes the way of defining constructor in the xml document and retrieving the values defined in the constructor using java file.




  <entry key="CompanyName" value=""/>   <entry key="Address" value="Rohini"/>

<constructor-arg>:-This is the way to declare  the constructor injection consisting of arguments. The use of this injection is to tell the container that the application wants to use the Constructor Injection.
<entry key="CompanyName" value=""/>:-
Passing the value to the constructor is usually done through the <value>element. Here we have pass " " as the company name.


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

  <bean id="encyclopedia"
  <entry key="CompanyName" value=""/>
  <entry key="Address" value="Rohini"/>

  <bean id="company" class="Companyinformation">
  <property name="encyclopedia" ref="mybean"/>

Here is the file named through which we are retrieving the properties of the bean which we have defined in the above file i.e. context.xml

 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-Here we are creating an instance of the XmlBeanFactory which is used to read bean definition from an XML document
new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.util.Assert;
public class Main {
  public static void main(String[] a) {
  XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
  company mycomp = (companybeanFactory.getBean("company");
  System.out.println("Name of the company is: " + mycomp.Name());
  System.out.println("Address of the company is: " + mycomp.address());
interface company {
 String Name();
 String address();
interface Detail {
  String find(String entry);
class Companyinformation implements company {
  private Detail detail;
  public String Name() {
  String name = this.detail.find("CompanyName");
  return String.valueOf(name);
  public String address() {
  String add = this.detail.find("Address");
  return String.valueOf(add);
  public void setEncyclopedia(Detail d) {
  this.detail = d;
class Configure implements Detail {
  private Map map;
  public Configure(Map map) {
  Assert.notNull(map, "Arguments cannot be null."); = map;
  public String find(String s) {
  return (;

Output of the program

Nov 26, 2008 12:48:55 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Name of the company is:
Address of the company is: Rohini
BUILD SUCCESSFUL (total time: 1 second)

Download source code


Share on Google+Share on Google+

Calling Constructor in Spring

Posted on: May 25, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Calling Constructor in Spring   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Steve White
April 17, 2011
libraries for build and run

build: /usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar run: .:/usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar:/usr/share/java/commons-logging.jar
December 7, 2011
Calling Constructor in Spring

where did we call d constructor in d above example