2:09 am - Saturday January 21, 2017

Spring Inheritance Example Program in Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure

Jar Files 

 How To Add Jar Files:  

 

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="parentId" class="com.candidjava.EmployeeBean" abstract="true">
		<property name="employeeId">
			<value>1</value>
		</property>
		<property name="employeeName">
			<value>kalai</value>
		</property>
	</bean>

	<bean id="ManagerClass" class="com.candidjava.ManagerBean" parent="parentId">
		<property name="managerDept">
			<value>It</value>
		</property>
	</bean>

	<bean id="DeveloperClass" class="com.candidjava.DeveloperBean"
		parent="parentId">
		<property name="domain">
			<value>java</value>
		</property>
	</bean>
</beans>

DeveloperBean.java

package com.candidjava;

public class DeveloperBean extends EmployeeBean {
	private String domain;

	public String getDomain() {
		return domain;
	}

	public void setDomain(String Domain) {
		domain = Domain;
	}

}

EmployeeBean.java

package com.candidjava;

public class EmployeeBean {
	private int employeeId;
	private String employeeName;

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int EmployeeId) {
		this.employeeId = EmployeeId;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String EmployeeName) {
		employeeName = EmployeeName;
	}

}

MainTest.java

package com.candidjava;

public class MainTest {
	public static void main(String a[]) {
		DeveloperBean dBean = ServiceFactory.getDomain();
		ManagerBean dBean1 = ServiceFactory.getmanagerDept();
		System.out.println("EmployeeId::" + dBean.getEmployeeId());
		System.out.println("EmployeeName::" + dBean.getEmployeeName());
		System.out.println("DeveloperDomain::" + dBean.getDomain());
		System.out.println("ManagerDept::" + dBean1.getmanagerDept());

	}

}

ManagerBean.java

package com.candidjava;

public class ManagerBean extends EmployeeBean {
	private String managerDept;

	public String getmanagerDept() {
		return managerDept;
	}

	public void setManagerDept(String ManagerDept) {
		managerDept = ManagerDept;
	}
}

ServiceFactory.java

package com.candidjava;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class ServiceFactory {
	public static BeanFactory factory = new XmlBeanFactory(
			new ClassPathResource("com\\candidjava\\spring-config.xml"));

	// private static String SERVICE="subClassOne";
	private static String SERVICE = "DeveloperClass";
	private static String SERVICE1 = "ManagerClass";

	public static DeveloperBean getDomain() {
		return (DeveloperBean) factory.getBean(ServiceFactory.SERVICE);
	}

	public static ManagerBean getmanagerDept() {
		return (ManagerBean) factory.getBean(ServiceFactory.SERVICE1);
	}

}

// It is not necessary to specify abstract=true for a parent bean. Any bean can
// be the parent bean of another. abstract=true causes that the parent bean
// itself will not be instantiated.

OUTPUT

 

DOWNLOAD +SOURCE CODE+JAR FILES

 

Filed in: Z-A Spring Codes

Comments are closed.