2011-02-09 24 views

Respuesta

14

Esto debería estar bien. Un contexto de Spring Bean le permite redefinir beans, con definiciones "posteriores" que reemplazan a las "anteriores". Esto debería aplicarse a los beans definidos por XML, así como a los beans definidos por anotaciones, incluso si están mezclados.

Por ejemplo, si usted tiene

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

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

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

En este caso, la definición XML de beanA debe prevalecer.

+2

Sin embargo, esto podría no funcionar para los campos '@ Autowired', según el tipo? Esto parece lanzar 'org.springframework.beans.factory.NoSuchBeanDefinitionException: no se define ningún bean único de tipo [com.xyz.BeanA]: bean de coincidencia única esperado, pero se encontró 2: [beanA, beanA]' (Spring 3.2) – Arjan

16

no sabía que la primavera puede mezclar configuraciones. aquí está el ejemplo detallado y muy útil.

Bean1 es el bean real que estamos configurando.

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

fuego es interfaz dependencia

package spring; 

public interface Fire { 

    public void fire(); 
} 

y aplicación maniquí 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()); 
    } 
} 

y aplicación maniquí 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

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

y clase principal

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

aquí es la salida

init 
class spring.FireImpl2 
nice 
destroy 

Aunque anotación resuelve la dependencia a FireImpl1, config XML overrided con FireImpl2. muy agradable.

Cuestiones relacionadas