12:47 pm - Saturday July 22, 2017

Spring Dependency Injection Setter 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:

 

 

 

 

 

 

 

 

 

 

JarFile1:

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles: 

   

  

   

   Spring- Beans.xml:

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

	<bean id="writer" class="com.candidjava.Writer">

		<property name="writeContent">
			<ref local="pdfWriter" />

		</property>

		<property name="writeXLFContent">
			<ref local="xlxWriter" />

		</property>

		<property name="writeDOCContent">
			<ref local="docWriter" />

		</property>

	</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());
		System.out.println(wObj.getWriter());
		System.out.println(wObj.getdocWriter());
	}

}

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;
	IWriter iwx;
	IWriter doc;

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

	public void setWriteXLFContent(IWriter writeContent) {
		this.iwx = writeContent;
	}

	public void setwriteDOCContent(IWriter writeContent) {
		this.doc = writeContent;
	}

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

	public String getWriter() {
		return iwx.write();
	}

	public String getdocWriter() {
		return doc.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:


DOWNLOAD+SOURCECODE+JARFILES

Filed in: Z-A Spring Codes

Comments are closed.