Spring中修改allowBeanDefinitionOverriding和allowC。。。

更新时间:2023-07-11 20:16:15 阅读: 评论:0

Spring中修改allowBeanDefinitionOverriding和allowC。。。allowBeanDefinitionOverriding属性含义
设置是否允许通过注册具有相同名称的不同定义来覆盖bean定义,并⾃动替换前者。否则,将引发异常。默认值为“true”。allowCircularReferences属性含义
设置是否允许bean之间的循环引⽤并⾃动尝试解析它们。
默认值为“true”。关闭此选项可在遇到循环引⽤时引发异常,完全不允许循环引⽤。
循环依赖应该都⽐较了解,主要看看allowBeanDefinitionOverriding。
public class TestSpring {
public static void main(String[] args){
ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("l","l");
Ur ur =(Ur) Bean("ur");安全灯
System.out.println("name: "+ ur.getName());
}
}
加载了两个配置⽂件,并且都配置了id为ur的bean对象。
<bean id="ur"class="com.wyl.learn.Ur">
<property name="name" value="lisi"></property>
<property name="age" value="18"></property>
</bean>
<bean id="ur"class="com.wyl.learn.Ur">
<property name="name" value="wangwu"></property>
intheafternoon
<property name="age" value="18"></property>
</bean>
根据allowBeanDefinitionOverriding属性的定义,默认值为true,并且相同名称的bean会进⾏覆盖,所以按照xml⽂件的加载顺序,bean2会覆盖bean1的配置。
最终输出结果为:name: wangwu
如果交换配置⽂件的加载顺序
ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("l","l");
最终输出结果为:name: lisi
如何修改属性值
⽅式⼀:
⾃定义⼀个applicationContext,并继承ClassPathXmlApplicationContext,重写customizeBeanFactor
y⽅法。
public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
public configLocations){
super(configLocations);
}
在线英英词典
@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory){
super.tAllowBeanDefinitionOverriding(fal);
super.tAllowCircularReferences(fal);
super.customizeBeanFactory(beanFactory);
}
}
public class TestSpring {
public static void main(String[] args){
MyClassPathXmlApplicationContext applicationContext =new MyClassPathXmlApplicationContext("l","l");        Ur ur =(Ur) Bean("ur");
System.out.println("name: "+ ur.getName());
}
}
报错如下,不允许出现定义同名的bean了。
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to register bean defi nition with name 'ur'
Offending resource:class path resource [l]; nested exception is org.springframework.beans.factory.support.BeanDefinitionOverrideException: Inv alid bean definition with name 'ur' defined in class path resource [l]: Cannot register bean definition [Generic bean:class[com.wyl.learn.Ur]; scope=;abstract=fal; lazyInit=fal; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=fal; factoryBeanName=null; factoryMeth odName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [l]]for bean 'ur': There is already [Generic bean :class[com.wyl.learn.Ur]; scope=;abstract=fal; lazyInit=fal; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=fal; factory BeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [l]] bound.
at org.springframework.beans.factory.(FailFastProblemReporter.java:72)
at org.springframework.beans.factory.(ReaderContext.java:119)
at org.springframework.beans.factory.(ReaderContext.java:104)
at org.springframework.l.DefaultBeanDefinitionDocumentReader.processBeanDefin
ition(DefaultBeanDefinitionDocumentReader.java:314 )
starpadat org.springframework.l.DefaultBeanDefinitionDocumentReader.parDefaultElement(DefaultBeanDefinitionDocumentReader.java:197) at org.springframework.l.DefaultBeanDefinitionDocumentReader.parBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:176) at org.springframework.l.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java: 149)
at org.springframework.isterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96) at org.springframework.isterBeanDefinitions(XmlBeanDefinitionReader.java:514)
at org.springframework.l.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:394)
at org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanD
efinitionReader.java:337)
at org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:305)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
at t.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
at t.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
at t.freshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at t.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:637)
at t.fresh(AbstractApplicationContext.java:522)
sit downat t.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at t.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:95)
at com.wyl.learn.MyClassPathXmlApplicationContext.<init>(MyClassPathXmlApplicationContext.java:9)
at com.wyl.learn.TestSpring.main(TestSpring.java:5)
Caud by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'ur' defined in class path res ource [l]: Cannot register bean definition [Generic bean:class[com.wyl.learn.Ur]; scope=;abstract=fal; lazyInit=fal; autowireMode=0; depe ndencyCheck=0; autowireCandidate=true; primary=fal; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=n ull; defined in class path resource [l]]for bean 'ur': There is already [Generic bean:class[com.wyl.learn.Ur]; scope=;abstract=fal; lazyInit= fal; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=fal; factoryBeanName=null; factoryMethodName=null; initMethodName= null; destroyMethodName=null; defined in class path resource [l]] bound.
at org.springframework.beans.factory.isterBeanDefinition(DefaultListableBeanFactory.java:927)
at org.springframework.beans.factory.isterBeanDefinition(BeanDefinitionReaderUtils.java:166)
sharpratio
at org.springframework.l.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:311 )
...21 morelyc
⽅式⼆:
直接调⽤t⽅法,并重新调⽤refresh()⽅法让属性⽣效,直接会出出现和⽅式⼀同样的报错信息。
public class TestSpring {
public static void main(String[] args){
ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("l","l");
applicationContext.tAllowBeanDefinitionOverriding(fal);
applicationContext.tAllowCircularReferences(fal);
Ur ur =(Ur) Bean("ur");
System.out.println("name: "+ ur.getName());
}
}
原理分析
两种⽅式的关键都在于customizeBeanFactory⽅法
化妆课程班
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory){
if(this.allowBeanDefinitionOverriding != null){
beanFactory.tAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
if(this.allowCircularReferences != null){
beanFactory.tAllowCircularReferences(this.allowCircularReferences);
}
}
spirng在加载过程中,会执⾏到这段代码,⾸先判断allowBeanDefinitionOverriding属性是否为null,⽽这两个属性默认情况下是为null。(包装类型定义的,所以默认为null),所以
beanFactory.tAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);就不会被执⾏,
那么在DefaultListableBeanFactory中这两个属性的地⽅就会取默认值true。(真正使⽤时是通过DefaultListableBeanFactory类中定义的这个两个属性值获取的)
AbstractRefreshableApplicationContext提供了修改属性的⽅法,如果重写了⽅法,那么修改的是DefaultListableBeanFactory 中的属性。
惊喜英语怎么写
DefaultListableBeanFactory中的属性,默认为true。
所以第⼆种⽅式的原理就是,先通过t⽅式,让两个属性值不为null,那么就会修改DefaultListableBeanFactory中的属性了。
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory){
//不为null,成⽴,进⼊if条件修改beanFactory.tAllowBeanDefinitionOverriding(true)
if(this.allowBeanDefinitionOverriding != null){
beanFactory.tAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
nobile}
if(this.allowCircularReferences != null){
beanFactory.tAllowCircularReferences(this.allowCircularReferences);
}
}
⽽第⼀种⽅式避免了重新调⽤refresh的过程,是直接在customizeBeanFactory中进⾏属性赋值,赋值完成后再调⽤⽗类的customizeBeanFactory⽅法继续完成之后的⼯作,有点类似静态代理模式的运⽤,重写⽗类⽅法,完成⾃⼰的业务逻辑之后再调⽤⽗类原本的⽅法。

本文发布于:2023-07-11 20:16:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/174421.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   允许   定义   循环   是否   加载   赋值
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图