Spring框架能否通过XML配置覆盖基于注解的配置?

19

Spring框架能否使用基于XML的配置覆盖基于注解的配置?我需要更改一个已通过注解定义且我不是该bean的作者的依赖项。

2个回答

18

我不知道Spring可以混合配置。这里有一个详细而且非常有用的例子。

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

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

配置文件.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

虽然注解解决了对FireImpl1的依赖,但XML配置却被FireImpl2覆盖了。 非常好。


15

这应该是可以的。Spring bean context 允许您重新定义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: No unique bean of type [com.xyz.BeanA] is defined: expected single matching bean but found 2: [beanA,beanA](Spring 3.2)。 - Arjan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接