企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在终端输入以下命令: ```shell git checkout -b 05 ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/BeanReference.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; public class BeanReference { private String name; private Object bean; public BeanReference(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Object getBean() { return bean; } public void setBean(Object bean) { this.bean = bean; } } ``` 为 `BeanFactory.java` 的 `getBean` 方法抛出异常,添加 `throws Exception` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/AbstractBeanFactory.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.factory; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import net.zhaoxuyang.ioc.bean.BeanDefinition; public abstract class AbstractBeanFactory implements BeanFactory { private Map<String, BeanDefinition> beanDefinitionMap; private final List<String> beanDefinitionNames = new LinkedList<>(); public AbstractBeanFactory() { beanDefinitionMap = new ConcurrentHashMap<>(16); } @Override public Object getBean(String name) throws Exception { BeanDefinition bd = beanDefinitionMap.get(name); if (bd == null) { throw new IllegalArgumentException(name); } Object bean = bd.getBean(); if (bean == null) { bean = doCreateBean(bd); } return bean; } @Override public void registerBeanDefinition(String name, BeanDefinition bd) throws Exception { beanDefinitionMap.put(name, bd); beanDefinitionNames.add(name); } public void preInstantiateSingletons() throws Exception { for (String beanName : beanDefinitionNames) { getBean(beanName); } } protected abstract Object doCreateBean(BeanDefinition beanDefinition) throws Exception; } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/AutowireCapableBeanFactory.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.factory; import java.lang.reflect.Field; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.BeanReference; import net.zhaoxuyang.ioc.bean.PropertyValue; public class AutowireCapableBeanFactory extends AbstractBeanFactory { @Override protected Object doCreateBean(BeanDefinition beanDefinition) throws Exception { Object bean = createBeanInstance(beanDefinition); beanDefinition.setBean(bean); 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); Object value = pv.getValue(); if (value instanceof BeanReference) { BeanReference beanReference = (BeanReference) value; value = getBean(beanReference.getName()); } declareField.set(bean, value); } } } ``` 修改 `XmlBeanDefinitionReader.java` 的 `processProperty` 方法: ```java private void processProperty(Element ele,BeanDefinition beanDefinition) { NodeList propertyNode = ele.getElementsByTagName("property"); for (int i = 0; i < propertyNode.getLength(); i++) { Node node = propertyNode.item(i); if (node instanceof Element) { Element propertyEle = (Element) node; String name = propertyEle.getAttribute("name"); String value = propertyEle.getAttribute("value"); if(value != null && value.length()>0){ beanDefinition.getPropertyValues() .addPropertyValue(new PropertyValue(name, value)); } else { String ref = propertyEle.getAttribute("ref"); if(ref == null || ref.length()==0){ throw new IllegalArgumentException(name); } BeanReference bf = new BeanReference(ref); beanDefinition.getPropertyValues() .addPropertyValue(new PropertyValue(name,bf)); } } } } ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/OutputService.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; import org.junit.Assert; public class OutputService { private TestService testService; public void output(String text){ Assert.assertNotNull(testService); System.out.println(text); } public void setTestService(TestService testService) { this.testService = testService; } } ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/TestService.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; public class TestService { private OutputService outputService; private String text; public void echo() { System.out.println(text); } public void setText(String text) { this.text = text; } public OutputService getOutputService() { return outputService; } public void setOutputService(OutputService outputService) { this.outputService = outputService; } } ``` 创建文件 `/Code/z8g/ioc/src/test/resources/ioc.xml` ,其内容为: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean name="testService" class="net.zhaoxuyang.ioc.bean.TestService"> <property name="text" value="Hello World!"></property> <property name="outputService" ref="outputService"></property> </bean> <bean name="outputService" class="net.zhaoxuyang.ioc.bean.OutputService"> <property name="testService" ref="testService"></property> </bean> </beans> ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/BeanFactoryTest.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; import java.util.Map; import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory; import net.zhaoxuyang.ioc.bean.factory.AutowireCapableBeanFactory; import net.zhaoxuyang.ioc.bean.io.ResourceLoader; import net.zhaoxuyang.ioc.bean.xml.XmlBeanDefinitionReader; import org.junit.Test; public class BeanFactoryTest { @Test public void testGetBean() throws Exception { // 1.读取配置 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions("ioc.xml"); // 2. 初始化BeanFactory并注册bean AbstractBeanFactory beanFactory = new AutowireCapableBeanFactory(); for(Map.Entry<String,BeanDefinition> entry:reader.getRegistry().entrySet()){ beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); } // 3. 获取bean TestService service = (TestService) beanFactory.getBean("testService"); service.echo(); } @Test public void testPreInstantiate() throws Exception{ // 1. 读取配置 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions("ioc.xml"); // 2. 初始化BeanFactory并注册bean AbstractBeanFactory beanFactory = new AutowireCapableBeanFactory(); for(Map.Entry<String,BeanDefinition> entry:reader.getRegistry().entrySet()){ beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); } // 3. 初始化bean beanFactory.preInstantiateSingletons(); // 4. 获取bean TestService testService = (TestService) beanFactory.getBean("testService"); testService.echo(); } } ``` 在终端执行以下命令: ```shell $ git add . $ git commit -m 'inject-bean-to-bean' ```