What is Setter injection in Spring ?
The Spring IoC container supports setter injection, which is the preferred method of dependency injection in Spring. Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config.
From a configuration standpoint, setter injection is easier to read because the property name being set is assigned as an attribute to the bean, along with the value being injected.
For more information,visit the following link:
Hi In Spring by using setter injection we set the value of instance variable of an object from the xml file this gives the flexibility to change the value without compiling the class
For this you have to configure the your bean class into the XML bean file suppose you have a student bean class then you can configure this class in bean xml file as
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 0 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 0 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="net.roseindia.StudentBean"> <property name="roll" value="1" /> <property name="name" value="Vinay Kumar" /> <property name="course" value="MAC" /> </bean> </beans>
In the bean class there are there instance variable roll, name and course...
then call the xml file in your application as
ApplicationContext context = new ClassPathXmlApplicationContext( "studentBean.xml"); StudentBean bean = (StudentBean) context.getBean("student"); System.out.println(bean);
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.