Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i did not know that spring can mix configurations. here is the detailed and very useful example.</p> <p>Bean1 is the actual bean we're configuring. </p> <pre><code>package spring; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Bean1 { private String naber; @Autowired @Qualifier("FireImpl1") private Fire fire; @PostConstruct public void init() { System.out.println("init"); getFire().fire(); } @PreDestroy public void destroy() { System.out.println("destroy"); } public void setNaber(String naber) { this.naber = naber; } public String getNaber() { return naber; } public void setFire(Fire fire) { this.fire = fire; } public Fire getFire() { return fire; } } </code></pre> <p>Fire is dependency interface</p> <pre><code>package spring; public interface Fire { public void fire(); } </code></pre> <p>and dummy implementation 1</p> <pre><code>package spring; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component @Qualifier("FireImpl1") public class FireImpl1 implements Fire { public void fire() { System.out.println(getClass()); } } </code></pre> <p>and dummy implementation 2</p> <pre><code>package spring; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component @Qualifier("FireImpl2") public class FireImpl2 implements Fire { public void fire() { System.out.println(getClass()); } } </code></pre> <p>config.xml</p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt; &lt;context:component-scan base-package="spring" /&gt; &lt;bean id="bean1" class="spring.Bean1"&gt; &lt;property name="naber" value="nice" /&gt; &lt;property name="fire" ref="fireImpl2" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>and main class</p> <pre><code>package spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Spring { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/config.xml"); applicationContext.registerShutdownHook(); Bean1 bean = (Bean1) applicationContext.getBean("bean1"); System.out.println(bean.getNaber()); } } </code></pre> <p>here is the output</p> <pre><code>init class spring.FireImpl2 nice destroy </code></pre> <p>Although annotation resolves dependency to FireImpl1, xml config overrided with FireImpl2. very nice.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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