码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-09 17:34:24      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

前导:开始学习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();
	}
}


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

标签:

原文地址:http://blog.csdn.net/u011345136/article/details/45602421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!