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(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。