12:47 pm - Saturday July 22, 2017

Spring Autowiring byType 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:

   

 

          

Autowiring.xml:


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="circle" class="com.candidjava.sam.Circle" />
	<bean id="triangle" class="com.candidjava.sam.Triangle" />
	<bean id="targetByType" autowire="byType" class="com.candidjava.Shape" />

</beans>

Shape.java:

package com.candidjava;

import com.candidjava.sam.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Shape {
	private Circle circle;
	private Triangle triangle;
	private Triangle triangle1;

	public void setCircle(Triangle triangle) {
		this.triangle = triangle;
		System.out.println("Property for Triangle is set");
	}

	public void setTriangle(Circle circle) {
		System.out.println("Property for Circle is set");
	}

	public void setDisplay(Circle circle) {
		System.out.println("This is display method");
	}

	public static void main(String[] args) {
		BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
				"config\\autowiring.xml"));
		Shape shape = null;
		System.out.println("\nUsing byType:\n");
		shape = (Shape) factory.getBean("targetByType");
	}
}

Circle.java:

package com.candidjava.sam;

public class Circle {

}

Triangle.java:

package com.candidjava.sam;

public class Triangle {

}

OUTPUT:

DOWNLOAD+SOURCECODE+JARFILES

Filed in: Z-A Spring Codes

Comments are closed.