企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在终端输入以下命令: ```shell $ git checkout -b 03 ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/PropertyValue.java` ,其内容为: ```java package net.zhaoxuyang.ioc.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; } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/PropertyValues.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class PropertyValues { private final List<PropertyValue> list = new ArrayList<>(10); public PropertyValues() { } public void addPropertyValue(PropertyValue pv) { list.add(pv); } public List<PropertyValue> getList() { return Collections.unmodifiableList(list); } } ``` 在 `BeanDefinition.java` 中新增以下内容: ```java private PropertyValues propertyValues = new PropertyValues(); public PropertyValues getPropertyValues() { return propertyValues; } public void setPropertyValues(PropertyValues propertyValues) { this.propertyValues = propertyValues; } ``` 修改 `AutowireCapableBeanFactory.java` ,修改后的内容为: ```java package net.zhaoxuyang.ioc.bean; import java.lang.reflect.Field; 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 pv:mbd.getPropertyValues().getList()){ Field declareField = bean.getClass().getDeclaredField(pv.getName()); declareField.setAccessible(true); declareField.set(bean, pv.getValue()); } } } ``` 同时在以下方法中抛出异常,即在方法后添加 `throws Exception`: - `BeanFactory.java` 的 `registerBeanDefinition` - `AbstractBeanFactory` 的 `registerBeanDefinition` 和 `doCreateBean` 修改 `TestService.java` ,修改后的内容为: ```java package net.zhaoxuyang.ioc.bean; public class TestService { private String text; public void echo() { System.out.println(text); } public void setText(String text) { this.text = text; } } ``` 修改 `BeanFactoryTest.java` ,修改后的内容为: ```java package net.zhaoxuyang.ioc.bean; import org.junit.Test; public class BeanFactoryTest { @Test public void testGetBean() throws Exception { // [1] 初始化beanFactory BeanFactory beanFactory = new AutowireCapableBeanFactory(); // [2] bean定义 BeanDefinition beanDefinition = new BeanDefinition(); beanDefinition.setBeanClassName("net.zhaoxuyang.ioc.bean.TestService"); // [3] 设置属性 PropertyValues pvs = new PropertyValues(); pvs.addPropertyValue(new PropertyValue("text", "Hello")); beanDefinition.setPropertyValues(pvs); // [4] 注册bean beanFactory.registerBeanDefinition("testService", beanDefinition); // [5] 获取bean TestService service = (TestService) beanFactory.getBean("testService"); service.echo(); } } ``` 在终端输入命令: ```shell $ git add . $ git commit -m 'inject-bean-with-property' ```