11:00 am - Thursday April 27, 2017

Spring Dependency Injection Constructor Based 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

ProjectStructure:

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 HowToAddJarFiles:

 

beans.xml:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="pdfWriter"></bean>
	<bean id="xlxWriter"></bean>
	<bean id="docWriter"></bean>

	<bean id="writer">

		<constructor-arg ref="xlxWriter" />

	</bean>

</beans>

 

DocWriter.java:

package com.candidjava;

public class DocWriter implements IWriter {

	@Override
	public String write() {
		// TODO Auto-generated method stub
		return "This is Doc Writer ";
	}

}

IWriter.java:

package com.candidjava;

public interface IWriter {
	public String write();
}

Main.java:

package com.candidjava;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext appObj = new FileSystemXmlApplicationContext(
				"config/beans.xml");
		Writer wObj = (Writer) appObj.getBean("writer");
		System.out.println(wObj.getWrite());
	}

}

PdfWriter.java:

package com.candidjava;

public class PdfWriter implements IWriter {
	@Override
	public String write() {
		// TODO Auto-generated method stub
		return "This is Pdf Writer ";
	}
}

Writer.java:

package com.candidjava;

public class Writer {

	IWriter iw;

	public Writer(IWriter writeContent) {
		this.iw = writeContent;
	}

	public String getWrite() {
		return iw.write();
	}
}

XlsWriter.java:

package com.candidjava;

public class XlsWriter implements IWriter {
	@Override
	public String write() {
		// TODO Auto-generated method stub
		return "This is Xls Writer ";
	}
}


Output.jpg:

 

DOWNLOAD+SOURCECODE+JARFILES


							
							
			
Filed in: Z-A Spring Codes

Comments are closed.