Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring is picking an interface implementation out of many, on its own?
    primarykey
    data
    text
    <p>Friends below is my code, I am trying to run dependency Injection with Spring</p> <p>I have an interface, two class implementations of that interface.</p> <p>One bean.xml and One main method class.</p> <p>Interface IWriter.java</p> <pre><code>package DI; public interface IWriter { public void writer(String s); } </code></pre> <p>Class Writer.java</p> <pre><code> package DI; import org.springframework.stereotype.Service; @Service public class Writer implements IWriter { public void writer (String s){ System.out.println(s); } } </code></pre> <p>Class NiceWriter.java</p> <pre><code>package DI; import org.springframework.stereotype.Service; @Service public class NiceWriter implements IWriter { public void writer (String s){ System.out.println("The string is " + s); } } </code></pre> <p>Another class</p> <pre><code>package DI; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class MySpringBeanWithDependency { @Autowired private IWriter writer; public void run() { String s = "This is my test"; writer.writer(s); } } </code></pre> <p>Main.java</p> <pre><code>package DI; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import DI.MySpringBeanWithDependency; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); BeanFactory factory = context; MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory.getBean("mySpringBeanWithDependency"); test.run(); } } </code></pre> <p>bean.xml</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt; &lt;context:component-scan base-package="DI" /&gt; &lt;/beans&gt; </code></pre> <p>When I run the code Spring container gives the output of the method of Writer.java class. I haven't anywhere specified which implementation to pick. How is Spring picking up the implementation of Writer.java??</p>
    singulars
    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.
 

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