12:47 pm - Saturday July 22, 2017

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

 

 

 

   

 Spring-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="targetByName" autowire="byName" 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 Triangle triangle;
	private Circle circle;

	public Shape() {

	}

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

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

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

		Shape t = null;

		System.out.println("Using byName:\n");
		t = (Shape) factory.getBean("targetByName");

	}
}

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.