Thursday, April 19, 2012

Spring IOC construtor based Injection Example

package com.venkat.spring;

public class Car {
   
    private String make;
    private String modal;
    private String tagNumber;

    public Car(String make, String modal, String tagNumber) {
        super();
        this.make = make;
        this.modal = modal;
        this.tagNumber = tagNumber;
    }

    @Override
    public String toString() {
        return "Car [make=" + make + ", modal=" + modal + ", tagNumber="
                + tagNumber + "]";
    }
   
}
----------------------------------------------------------
package com.venkat.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CarTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Car c=(Car)context.getBean("carBean");
System.out.println(c);
    }

}
-----------------------------------------------------------------

<?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.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="carBean" class="com.venkat.spring.Car">
<constructor-arg name="make" value="Infiniti"></constructor-arg>
<constructor-arg name="modal" value="G35"></constructor-arg>
<constructor-arg name="tagNumber" value="Venkat 007"></constructor-arg>
</bean>

</beans>

No comments:

Post a Comment