spring框架可以用基于XML的配置覆盖基于注释的配置吗?
spring框架可以用基于XML的配置覆盖基于注释的配置吗?我需要更改已通过注释定义的bean的依赖项,并且我不是该bean的作者。这应该可以。Springbean上下文允许您重新定义bean,“以后的”定义覆盖“以前的”。这应该适用于XML定义的bean以及注释定义的bean,即使它们是混合的 例如,如果你有spring框架可以用基于XML的配置覆盖基于注释的配置吗?,spring,configuration,annotations,xml-configuration,Spring,Configuration,Annotations,Xml Configuration,spring框架可以用基于XML的配置覆盖基于注释的配置吗?我需要更改已通过注释定义的bean的依赖项,并且我不是该bean的作者。这应该可以。Springbean上下文允许您重新定义bean,“以后的”定义覆盖“以前的”。这应该适用于XML定义的bean以及注释定义的bean,即使它们是混合的 例如,如果你有 @Configuration public class MyAnnotatedConfig { @Bean public Object beanA() { ..
@Configuration
public class MyAnnotatedConfig {
@Bean
public Object beanA() {
...
}
}
在这种情况下,beanA的XML定义应该优先。我不知道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());
}
}
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>
这是输出
init
class spring.FireImpl2
nice
destroy
尽管注释将依赖关系解析为FireImpl1,但xml配置被FireImpl2覆盖。
非常好。但是,根据类型,这可能不适用于
@Autowired
字段?这似乎抛出了org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义类型为[com.xyz.BeanA]的唯一bean:预期为单个匹配bean,但找到了2:[BeanA,BeanA]
(Spring 3.2)
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