Spring Framework

Spring Projects

Spring Project 1

Dependency on primitive value example
Previous Home Next

Introduction:In the case of dependency on primitive value, The constructor-arg is the sub element of <bean ......> is used to specified the dependency of a bean i.e to be satisfied to a constructor this elements uses values and reference attribute which are used to specified value primitive type. Technology use to run this source code

  1. Spring 2.5
  2. Eclipse
  3. JDK 1.6

Source Code

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.Dependency on primitive value 
examplespringframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="ab" class="org.r4r.A">
<constructor-arg value="101" />
<constructor-arg value="102"  type="int"/>
</bean>
</beans>

 

A.java

package org.r4r;
public class A {
int a;
String b;
public A(int a,String b) {
super();
this.a=a;
this.b =b;
System.out.println("Constructor is invoked");
}
public void display() {
System.out.println(a+"\t\t"+b);
}
}

Test.java

package org.r4r;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String ar[]){
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
System.out.println("Constructor value are following");
A a1=(A)f.getBean("ab");
a1.display();
}
}

Output:

Constructor value are following
Constructor is invoked
102 101
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: