2:10 am - Saturday January 21, 2017

Spring Autowiring constructor 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="targetConstructor" autowire="constructor" class="com.candidjava.Shape" />
</beans>

 

 Shape.java:

package com.candidjava;

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

import com.candidjava.sam.Triangle;
import com.candidjava.sam.Circle;

public class Shape {

	private Circle circle;
	private Triangle Triangle;

	public Shape(Circle circle) {
		System.out.println("This is circle");
	}

	public Shape(Triangle triangle) {
		System.out.println("This is Triangle");

	}

	public Shape(Triangle triangle, Circle circle) {
		System.out.println("This is both circle and triangle");
	}

	public static void main(String[] args) {
		BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
				"config\\autowiring.xml"));

		Shape shape = null;

		System.out.println("\nUsing constructor:\n");
		shape = (Shape) factory.getBean("targetConstructor");

	}
}

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.