Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Calling Constructor in Spring 
 

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

 

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.

 

 

 

<constructor-arg>             
        <util:map>
        <entry key="CompanyName" value="Roseindia.net"/>         <entry key="Address" value="Rohini"/>
        </util:map>
</constructor-arg>

<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="Roseindia.net"/>:-
Passing the value to the constructor is usually done through the <value>element. Here we have pass "Roseindia.net " as the company name.

context.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="encyclopedia"
          name="mybean"
          class="Configure">
        <constructor-arg>
            <util:map>
                <entry key="CompanyName" value="Roseindia.net"/>
                <entry key="Address" value="Rohini"/>
            </util:map>
        </constructor-arg>
    </bean>

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

Here is the file named Main.java 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.

Main.java

import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
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.");
        this.map = map;
    }

    public String find(String s) {
        return (Stringthis.map.get(s);
    }
}

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: Roseindia.net
Address of the company is: Rohini
BUILD SUCCESSFUL (total time: 1 second)

Download source code

                         
» View all related tutorials
Related Tags: c spring development framework application io enterprise read applications app frame for work ram erp e li dev enter in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

U r a genius, Thanks for all the help,

Posted by Sreenivasan on Wednesday, 04.8.09 @ 17:51pm | #86650

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.