2011-02-09 15 views

答えて

14

これは問題ありません。 Spring Beanのコンテキストでは、Beanを再定義することができます。 "後の"定義は、 "前のもの"を上書きします。これはXML定義のBeanとアノテーション定義のBeanが混在していても適用されます。あなたはこの場合、

@Configuration 
public class MyAnnotatedConfig { 
    @Bean 
    public Object beanA() { 
     ... 
    } 
} 

<bean class="com.xyz.MyAnnotatedConfig"/> 

<bean id="beanA" class="com.xyz.BeanA"/> 

を持っている場合、例えば

beanAのXML定義が優先されなければなりません。

+2

しかし、これはタイプに基づいて '@ Autowired'フィールドでは機能しないかもしれませんか?これはorg.springframework.beans.factory.NoSuchBeanDefinitionExceptionをスローするようです:[com.xyz.BeanA]型の一意のBeanが定義されていません:期待される単一の一致するBeanですが、見つかった2:[beanA、beanA] '(Spring 3.2) – Arjan

16

私は春が構成を混ぜることができるのか分かりませんでした。ここでは、詳細で非常に有用な例です。

Bean1は設定している実際のBeanです。

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; 
    } 
} 

火災依存インタフェース

package spring; 

public interface Fire { 

    public void fire(); 
} 

ダミー実装1

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()); 
    } 
} 

ダミー実装である2

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()); 
    } 
} 

config.xmlの

注釈がFireImpl1、FireImpl2でオーバーライドXML設定に依存性を解決しますが、ここ
<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"> 
    <context:component-scan base-package="spring" /> 
    <bean id="bean1" class="spring.Bean1"> 
     <property name="naber" value="nice" /> 
     <property name="fire" ref="fireImpl2" /> 
    </bean> 
</beans> 

とメインクラス

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()); 
    } 
} 

は出力

init 
class spring.FireImpl2 
nice 
destroy 

です。 非常に良い。

関連する問題