Note that there are some explanatory texts on larger screens.

plurals
  1. POAutowiring with constructor
    primarykey
    data
    text
    <p>Spring doc for Autowiring by constructor says that its same as byType, but for constructor arguments. </p> <p>If thats the case, then why doesnt the following program fail: </p> <p><strong>config.xml</strong></p> <pre><code>&lt;bean id="traingle" class="org.practise.autowiring.Triangle" autowire="constructor" /&gt; &lt;bean id="pointA" class="org.practise.autowiring.Point"&gt; &lt;property name="x" value="0"/&gt; &lt;property name="y" value="10"/&gt; &lt;/bean&gt; &lt;bean id="pointB" class="org.practise.autowiring.Point"&gt; &lt;property name="x" value="220"/&gt; &lt;property name="y" value="330"/&gt; &lt;/bean&gt; &lt;bean id="pointC" class="org.practise.autowiring.Point"&gt; &lt;property name="x" value="40"/&gt; &lt;property name="y" value="60"/&gt; &lt;/bean&gt; </code></pre> <p></p> <p>In the above case, I have 3 beans of same type(Point) that I'm trying to inject in the following class:</p> <pre><code> public class Triangle { private Point pointA; private Point pointB; private Point pointC; public Triangle(Point pointA, Point pointB, Point pointC) { this.pointA = pointA; this.pointB = pointB; this.pointC = pointC; } public Point getPointA() { return pointA; } public void setPointA(Point pointA) { this.pointA = pointA; } public Point getPointB() { return pointB; } public void setPointB(Point pointB) { this.pointB = pointB; } public Point getPointC() { return pointC; } public void setPointC(Point pointC) { this.pointC = pointC; } public void draw() { System.out.println("Point A: " + getPointA().getX() + ", " + getPointA().getY()); System.out.println("Point B: " + getPointB().getX() + ", " + getPointB().getY()); System.out.println("Point C: " + getPointC().getX() + ", " + getPointC().getY()); } } </code></pre> <p><strong>Point</strong></p> <pre><code>public class Point { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } </code></pre> <p>}</p> <p><strong>Executing the code:</strong></p> <pre><code>public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("org/practise/autowiring/spring-autowiring.xml"); Triangle triangle = (Triangle) context.getBean(Triangle.class); triangle.draw(); } </code></pre> <p>It passes and prints the following:</p> <pre><code>Aug 18, 2013 2:04:56 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@43ee1619: startup date [Sun Aug 18 14:04:56 PDT 2013]; root of context hierarchy Aug 18, 2013 2:04:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [org/practise/autowiring/spring-autowiring.xml] Aug 18, 2013 2:04:56 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@13c6395b: defining beans [traingle,pointA,pointB,pointC]; root of factory hierarchy Point A: 0, 10 Point B: 220, 330 Point C: 40, 60 </code></pre> <p>When I change the wiring type to "byType", it throws the expected exception. Only in the above case it works fine. I'm using Spring 3.2 </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload