Home Tutorial Spring Spring3 Ioc Spring Constructor arg index

 
 

Spring Constructor arg index
Posted on: August 2, 2010 at 12:00 AM
Advertisement
In this example you will see how inject the arguments into your bean according to the constructor argument index.

Constructor Arguments Index

In this example you will see how inject the arguments into your bean according to the constructor argument index.

ConstructorInjection.java 

package net.roseindia;

public class ConstructorInjection {
  private String name = null;
  private int roll;

  public ConstructorInjection(String name, int roll) {
    this.name = name;
    this.roll = roll;
  }

  public String getMessage() {
    return name;
  }

  public int getValue() {
    return roll;
  }
}

ConstructorInjectionTest.java

package net.roseindia;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConstructorInjectionTest {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
        "context.xml");
    ConstructorInjection bean = (ConstructorInjectionbeanfactory
        .getBean("springMessage");
    System.out.println(bean.getMessage());
    System.out.println(bean.getValue());
  }
}

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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
<bean id="springMessage" class="net.roseindia.ConstructorInjection">
   <constructor-arg index="0" value="Aakash Pandey" />
   <constructor-arg index="1" value="1001" />
  </bean>
</beans>

When you run this application it display output as shown below:

Aakash Pandey
1001

Download this example code

Advertisement

Related Tags for Spring Constructor arg index:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 2, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com