Thursday, April 19, 2012

Spring Setter based Injection Example

package com.venkat.spring;

public class Cricket {

    private String name;
    private int runs;
    private String country;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRuns() {
        return runs;
    }
    public void setRuns(int runs) {
        this.runs = runs;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "Cricket [name=" + name + ", runs=" + runs + ", country="
                + country + "]";
    }
   
}
------------------------------------------------------------------------------

package com.venkat.spring;

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

public class TestCricket {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    Cricket c=(Cricket)    context.getBean("cricketBean");
    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="cricketBean" class="com.venkat.spring.Cricket">
<property name="name" value="Sachin"></property>
<property name="runs" value="20000"></property>
<property name="country" value="India"></property>
</bean>

</beans>

No comments:

Post a Comment