Spring First Example, Setter Injection With Primitive Values

Let us see the first program in spring, with setter injection with some primitive values…
Files required..
1. WelcomeBean.java
2. ClientLogic.java [ Our logic ] 3. spconfig.xml [ spring configuration file ]

WelcomeBean.java

ClientLogic.java

spconfig.xml

Explanation
In WelcomeBean.java, Spring container, using  setter method for the property message (primitive), will inject some value in that property at run time
In ClientLogic.java first we need to load the configuration file,
And give this res object to BeanFactory [ Spring container ] with XmlBeanFactory, so now factory knows all the beans in the xml file so we can now call any bean with bean id.
In ClientLogic.java, if we call getBean(“id1″) then internally the spring framework executes the following statements
WelcomeBean welcomeBean = new WelcomeBean();
welcomeBean.setMessage(“Welcome to spring first exaple”);
And now will gives WelcomeBean object back [at line number– ,] in the form of Object class object, and i typecast into WelcomeBean class at line number —
Remember, by default every spring bean is the singleton class. Spring IOC container makes a spring bean as singleton automatically
Return type of getBean() is always super class object, which is Object class object

See in spconfig.xml, line number 7 we have written the property element right, here <property /> means we are saying to the spring container that we have written setter method in our bean class [WelcomeBean.java 7 to 9 lines ], in the that property we assigned value as an attribute, which means the setter injection is in the form of primitive values.

   

Comments are closed