已经完成 实现一个容器、定义和注册Bean、实例化Bean、按照是否包含构造函数实现不同的实例化策略,那么在创建对象实例化,我们还缺少什么?
类中是否有属性的问题
,如果类中包含属性那么在实例化的时候就需要把属性信息填充上,这样才是一个完整的对象创建。int、Long、String
,还包括没有实例化的对象属性,都需要在 Bean 创建时进行填充操作。技术设计:注入属性和依赖对象
newInstance
或者 cglib
创建后,开始补全属性信息,那么就可以在类 AbstractAutowireCapableBeanFactory
的 createBean
方法中添加补全属性方法。AbstractAutowireCapableBeanFactory#createBean
方法中添加 applyPropertyValues
操作。pom.xml
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.5.0version>
dependency>
spring-step-04
|-src
|-main
| |-java
| |-com.lino.springframework
| |-factory
| | |-config
| | | |-BeanDefinition.java
| | | |-BeanReference.java
| | | |-SingletonBeanRegistry.java
| | |-support
| | | |-AbstractAutowireCapableBeanFactory.java
| | | |-AbstractBeabFactory.java
| | | |-BeanDefinitionRegistry.java
| | | |-CglibSubclassingInstantiationStrategy.java
| | | |-DefaultListableBeanFactory.java
| | | |-DefaultSingletonBeanRegistry.java
| | | |-InstantiationStrategy.java
| | | |-SimpleInstantiationStrategy.java
| | |-BeanFactory.java
| |-BeansException.java
| |-PropertyValue.java
| |-PropertyValues.java
|-test
|-java
|-com.lino.springframework.test
|-bean
| |-UserDao.java
| |-UserService.java
|-ApiTest.java
createBean
中补全属性填充部分。PropertyValue.java
package com.lino.springframework;
/**
* @description: Bean属性信息
*/
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 name;
}
public Object getValue() {
return value;
}
}
PropertyValues.java
package com.lino.springframework;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 属性值集合
*/
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<>();
public void addPropertyValue(PropertyValue pv) {
this.propertyValueList.add(pv);
}
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue pv : this.propertyValueList) {
if (pv.getName().equals(propertyName)) {
return pv;
}
}
return null;
}
}
BeanReference.java
package com.lino.springframework.factory.config;
/**
* @description: Bean 引用
*/
public class BeanReference {
private final String beanName;
public BeanReference(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
}
for
循环时还得判断属性填充是否为空。AbstractAutowireCapableBeanFactory.java
package com.lino.springframework.factory.support;
import cn.hutool.core.bean.BeanUtil;
import com.lino.springframework.BeansException;
import com.lino.springframework.PropertyValue;
import com.lino.springframework.PropertyValues;
import com.lino.springframework.factory.config.BeanDefinition;
import com.lino.springframework.factory.config.BeanReference;
import java.lang.reflect.Constructor;
/**
* @description: 实现默认bean创建的抽象bean工厂超类
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 给bean填充属性
applyPropertyValues(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
registerSingletonBean(beanName, bean);
return bean;
}
private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if (value instanceof BeanReference) {
// A 依赖 B,获取 B 的实例化
BeanReference beanReference = (BeanReference) value;
value = getBean(beanReference.getBeanName());
}
// 属性填充
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception e) {
throw new BeansException("Error setting property values: " + beanName);
}
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
}
createBean
、createBeanInstance
、applyPropertyValues
,这里我们主要关注 createBean
的方法中调用的 applyPropertyValues
方法。applyPropertyValues
中,通过获取 beanDefinition.getPropertyValues()
循环进行属性填充操作。
BeanReference
,那么就需要递归获取 Bean 实例,调用 getBean
方法。UserDao.java
package com.lino.springframework.test.bean;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 模拟用户DAO类
*/
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "张三");
hashMap.put("10002", "李四");
hashMap.put("10003", "王五");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
UserServce.java
package com.lino.springframework.test.bean;
/**
* @description: 模拟用户 Bean 对象
*/
public class UserService {
private String uId;
private UserDao userDao;
/**
* 查询用户信息
*/
public void queryUserInfo() {
System.out.println("查询用户信息: " + userDao.queryUserName(uId));
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
UserService
中注入 UserDao
,这样就能体现出 Bean 属性的依赖。ApiTest.java
@Test
public void test_BeanFactory() {
// 1.初始化 BeanFactory
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 2.UserDao注册
beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));
// 3.UserService 设置属性[uId、userDao]
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("uId", "10001"));
propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("userDao")));
// 4.UserService 注入bean
BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);
beanFactory.registerBeanDefinition("userService", beanDefinition);
// 5.获取bean
UserService userService = (UserService) beanFactory.getBean("userService");
userService.queryUserInfo();
}
userDao
注入到 Bean 容器中。
beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class))
。new PropertyValue("uId", "10001")
。new PropertyValue("userDao", new BeanReference("userDao"))
。userService
对象,调用方法即可。测试结果
查询用户信息: 张三
userDao.queryUserName(uId)
。