Home  >  Spring Core

Setter Method can return object in Spring 3.1

By Arvind Rai, March 24, 2013
In spring 3.1, setter method of the bean can return object or any value. Before spring 3.1, it is not possible to use return method in setter method. In business requirement, it can be needed that setter method should return a value. Find sample example.

app-context.xml
<?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:c="http://www.springframework.org/schema/c"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="city" class="com.concretepage.City">
    <property name="cityName" value="Varanasi"/>
    <property name="area" value="1000"/> 
  </bean>
 
</beans>
 


City.java
package com.concretepage;
public class City     {
	private String cityName;

	public int setCityName(String cityName) {
		this.cityName = cityName;
		return 1;
	}
	
	public String getCityName() {
		return cityName;
	}
}
 


AppDemo.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppDemo {
    public static void main(String... args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");
            City st=(City) context.getBean("city");
            String cityName=st.getCityName();
            System.out.println("cityNmae:"+cityName);
    } 
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us