大佬教程收集整理的这篇文章主要介绍了java – @Autowired和default-autowire可以共存吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1个具有所需类型的bean,则bean的所有字段将自动注入依赖项.
问题是当使用注释时这是如何工作的,它是否有效.
我的测试显示,即使我使用
@resource(name="someConcreteFoo")
private Foo foo;
上下文尝试按类型自动装配字段,如果有多个Foo实现则失败.因此,对于我所看到的,default-autowire不会与注释混合.我在文档中找不到任何具体内容.
扩展问题 – 当使用Xml-only时,spring如何使用default-autowiring进行操作.即如果你有< property="">.属性注入是否覆盖默认值(应该是).
我可以做更多的测试,但我更喜欢某些引用确认的行为.任何见解?
/**
* Populate the bean instance in the given BeanWrapper with the property values
* FROM the bean deFinition.
* @param beAnname the name of the bean
* @param mbd the bean deFinition for the bean
* @param bw BeanWrapper with bean instance
*/
protected void populateBean(String beAnname,AbstractBeanDeFinition mbd,BeanWrapper bw) {
PropertyValues pvs = mbd.getPropertyValues();
if (bw == null) {
if (!pvs.isEmpty()) {
throw new BeanCreationException(
mbd.getresourceDescription(),beAnname,"CAnnot apply property values to null instance");
}
else {
// Skip property population phase for null instance.
return;
}
}
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used,for example,// to support styles of field injection.
Boolean conTinueWithPropertyPopulation = true;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(),beanName)) {
conTinueWithPropertyPopulation = false;
break;
}
}
}
}
if (!conTinueWithPropertyPopulation) {
return;
}
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_NAME ||
mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_Name) {
autowireByName(beAnname,mbd,bw,newPvs);
}
// Add property values based on autowire by type if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_TYPE) {
autowireByType(beAnname,newPvs);
}
pvs = newPvs;
}
Boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
Boolean needsDepcheck = (mbd.getDependencycheck() != RootBeanDeFinition.DEPENDENCY_checK_NONE);
if (hasInstAwareBpps || needsDepcheck) {
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencycheck(bw);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
pvs = ibp.postProcessPropertyValues(pvs,filteredPds,bw.getWrappedInstance(),beanName);
if (pvs == null) {
return;
}
}
}
}
if (needsDepcheck) {
checkDependencies(beAnname,pvs);
}
}
applyPropertyValues(beAnname,pvs);
}
我个人认为应用自动装配和InstantiationAwareBeanPostProcessor的顺序是错误的,因为@resource注释只会应用在postProcessPropertyValues中,因此在自动装配之后(此时自动装配已经失败).
现在我不知道是否会对更改调用顺序产生影响,因此在自动装配之前解析@resource注释,但这可能是一个引发错误/修复的东西(我使用以下方式加载我的测试应用程序上下文来解决此问题):
ApplicationContext ctx = new ClassPathXmlApplicationContext("test/appctx.xml") {
protected org.springframework.beans.factory.support.DefaultListablebeanfactory createbeanfactory() {
return new DefaultListablebeanfactory(geTinternalParentbeanfactory()) {
protected void populateBean(String beAnname,org.springframework.beans.factory.support.AbstractBeanDeFinition mbd,org.springframework.beans.beanWrapper bw) {
PropertyValues pvs = mbd.getPropertyValues();
if (bw == null) {
if (!pvs.isEmpty()) {
throw new BeanCreationException(
mbd.getresourceDescription(),"CAnnot apply property values to null instance");
}
else {
// Skip property population phase for null instance.
return;
}
}
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used,// to support styles of field injection.
Boolean conTinueWithPropertyPopulation = true;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(),beanName)) {
conTinueWithPropertyPopulation = false;
break;
}
}
}
}
if (!conTinueWithPropertyPopulation) {
return;
}
Boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
Boolean needsDepcheck = (mbd.getDependencycheck() != RootBeanDeFinition.DEPENDENCY_checK_NONE);
if (hasInstAwareBpps || needsDepcheck) {
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencycheck(bw);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
pvs = ibp.postProcessPropertyValues(pvs,beanName);
if (pvs == null) {
return;
}
}
}
}
if (needsDepcheck) {
checkDependencies(beAnname,pvs);
}
}
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_NAME ||
mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_Name) {
autowireByName(beAnname,newPvs);
}
// Add property values based on autowire by type if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDeFinition.AUTOWIRE_BY_TYPE) {
autowireByType(beAnname,newPvs);
}
pvs = newPvs;
}
applyPropertyValues(beAnname,pvs);
}
};
}
};
希望有所帮助
以上是大佬教程为你收集整理的java – @Autowired和default-autowire可以共存吗?全部内容,希望文章能够帮你解决java – @Autowired和default-autowire可以共存吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。