
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 { public void setAge(int age) { public String getName() { public void setName(String name) { public char getSex() { public void setSex(char sex) { public void printData(){ |
package com.visualbuilder.example; import org.springframework.context.ApplicationContext; import com.visualbuilder.beans.SetterInjectionBean; public class SetterInjectionExample { |
<bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean"> |
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
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin



