这节的话就更简单了,主要实现bean的单例(创建一个对象),多例(可创建多个对象),怎么可以实现如果单例的话第一次存储容器里,第二次获取从容器里获取就可呢,非常非常简单,直接进入代码吧
1.首先第一步是需要在BeanDefination里去添加设置Scope的值,等待解析xml的时候通过set注入到BeanDefination对象里就可以在处理时使用了。
public class BeanDefinition {
// 按上一章的话这里已经把Object改成了Class,这样就可以把bean的实例化操作放到容器中处理了
private Class beanClass;
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
private PropertyValues propertyValues;
// 初始化方法名称
private String initMethodName;
// 销毁方法名称
private String destroyMethodName;
// 目的把xml中的bean对象作用范围填充到属性中
private String scope = SCOPE_SINGLETON;
// 默认是单例
private boolean singleton = true;
private boolean prototype = false;
public void setScope(String scope) {
this.scope = scope;
this.singleton = SCOPE_SINGLETON.equals(scope);
this.prototype = SCOPE_PROTOTYPE.equals(scope);
}
public boolean isSingleton() {
return singleton;
}
public boolean isPrototype() {
return prototype;
}
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
// 此处加入new PropertyValues()目的是在Bean对象没有属性时后续获取会报空错,在此处理
this.propertyValues = new PropertyValues();
}
public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
this.beanClass = beanClass;
this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyVal

最低0.47元/天 解锁文章
1358

被折叠的 条评论
为什么被折叠?



