Spring核心学习(3)为Bean注入属性

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。


内容:1.Propertyvalue-保存属性注入信息。2.AutowireCapableBeanFactory-可自动装配的BeanFactory。

这里我们重新定义了BeanDefinition,增加了属性列表这个字段,我们将为bean附加额外的属性,所以我们又定了PropertyValues、PropertyValue来辅助,当我们为bean定义的时候同时设置他的属性,通过反射机制来动态的附加到bean里,来达到类似配置文件的效果。

BeanDefinition:

public class BeanDefinition {
	
	private Object bean;
	
	private Class beanClass;
	
	private String beanClassName;

	private PropertyValues propertyValues;
	
	public BeanDefinition() {}

	public Object getBean() {
		return bean;
	}

	public void setBean(Object bean) {
		this.bean = bean;
	}

	public Class getBeanClass() {
		return beanClass;
	}

	public void setBeanClass(Class beanClass) {
		this.beanClass = beanClass;
	}

	public String getBeanClassName() {
		return beanClassName;
	}

	public void setBeanClassName(String beanClassName) {
		this.beanClassName = beanClassName;
		try {
			this.beanClass = Class.forName(beanClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
	}

	public PropertyValues getPropertyValues() {
		return propertyValues;
	}

	public void setPropertyValues(PropertyValues propertyValues) {
		this.propertyValues = propertyValues;
	}
	
}
PropertyValue:

/**
 * 用户bean的属性注入
 * @author acer
 *
 */
public class PropertyValue {
	private final String name;
	private final Object value;
	
	public PropertyValue(String name, Object value) {
		this.name = name;
		this.value = value;
	}
	
	public String getName() {
		return this.name;
	}
	
	public Object getValue() {
		return this.value;
	}
}

PropertyValues:

/**
 * 包装一个对象所有的PropertyValue
 * @author acer
 *
 */
public class PropertyValues {
	private final List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>();
	
	public PropertyValues() {}
	
	public void addPropertyValue(PropertyValue pv) {
		//TODO:这里可以对于重复propertyName进行判断,直接用list没法做到
		this.propertyValueList.add(pv);
	}

	public List<PropertyValue> getPropertyValueList() {
		return propertyValueList;
	}
	
}


BeanFactory:

public interface BeanFactory {
	
	Object getBean(String name);
	
	void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception;
}

AbstractBeanFactory:

public abstract class AbstractBeanFactory implements BeanFactory{
	private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();

	@Override
	public Object getBean(String name) {
		return beanDefinitionMap.get(name).getBean();
	}

	@Override
	public void registerBeanDefinition(String name,
			BeanDefinition beanDefinition) throws Exception{
		Object bean = doCreateBean(beanDefinition);
		beanDefinition.setBean(bean);
		beanDefinitionMap.put(name, beanDefinition);
	}

	/**
     * 初始化bean
     * @param beanDefinition
     * @return
     */
    protected abstract Object doCreateBean(BeanDefinition beanDefinition) throws Exception;
}

AutowireCapableBeanFactory:

/**
 * 可自动装载内容的BeanFactory
 * @author acer
 *
 */
public class AutowireCapableBeanFactory extends AbstractBeanFactory{

	@Override
	protected Object doCreateBean(BeanDefinition beanDefinition) throws Exception{
		Object bean = createBeanInstance(beanDefinition);
		applyPropertyValues(bean, beanDefinition);
		return bean;
	}

	protected Object createBeanInstance(BeanDefinition beanDefinition) throws Exception {
		return beanDefinition.getBeanClass().newInstance();
	}
	
	protected void applyPropertyValues(Object bean, BeanDefinition mbd) throws Exception {
		for (PropertyValue propertyValue : mbd.getPropertyValues().getPropertyValueList()) {
			Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
			declaredField.setAccessible(true);
			declaredField.set(bean, propertyValue.getValue());
		}
	}
}

HelloWorldService:

public class HelloWorldService {

    private String text;

    public void helloWorld(){
        System.out.println(text);
    }

    public void setText(String text) {
        this.text = text;
    }
}

BeanFactoryTest:

public class BeanFactoryTest {

	@Test
	public void test() throws Exception{
		// 1.初始化beanFactory
		BeanFactory beanFactory = new AutowireCapableBeanFactory();
		
		// 2.bean的定义
		BeanDefinition beanDefinition = new BeanDefinition();
		beanDefinition.setBeanClassName("step3.test.HelloWorldService");
		
		// 3.设置属性
		PropertyValues propertyValues = new PropertyValues();
		propertyValues.addPropertyValue(new PropertyValue("text", "Hello World! from step3!"));
		beanDefinition.setPropertyValues(propertyValues);
		
		// 4.生成bean
		beanFactory.registerBeanDefinition("helloWorldService", beanDefinition);
		
		// 5.获取bean
		HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService");
		helloWorldService.helloWorld();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值