Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

"http://www.w3.org/TR/html4/loose.dtd">







Setter Injection In Spring


In JavaBean the properties are private and it has setter and getter method to access it. Spring can take advantage of a property’s
setter method to configure the property’s value through setter injection. In setter injection the property which need to set through the spring configuration file should have the setter method in the bean. Let's see with the help of example how the beans are wired through the setter injection.


Example:-



package com.visualbuilder.beans;


public class SetterInjectionBean {

    private String name;

    private int age;

    private char sex;



    public int getAge() {

           
return age;

       
}


        public void setAge(int age) {

           
this.age = age;

       
}


    public String getName() {

           
return name;

       
}


    public void setName(String name) {

           
this.name = name;

       
}


    public char getSex() {

           
return sex;

       
}


    public void setSex(char sex) {

           
this.sex = sex;

       
}


    public void printData(){

           
System.out.println( "Assigned Name is "+ name);

           
System.out.println( "Assigned Age is "+ age);

           
System.out.println( "Assigned Sex is "+ sex);



       
}

}


 





package com.visualbuilder.example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.SetterInjectionBean;


public class SetterInjectionExample {

    public static void main (String [] ar){

               
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

               
SetterInjectionBean bean = (SetterInjectionBean)context.getBean("setterInjection");

        bean.printData();

       
}

}


 




<bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean">

   
<property name="name" value="John Smith" />

   
<property name="age" value="25" />

   
<property name="sex" value="M" />

</bean>


Output:-


Jun 2, 2008 4:33:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 16:33:02 IST 2008]; root of context hierarchy

Jun 2, 2008 4:33:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 2, 2008 4:33:03 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330

afterPropertiesSet method is called

Jun 2, 2008 4:33:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M



 




                    

Copyright © 2012 VisualBuilder. All rights reserved