企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在终端输入命令: ```shell git checkout -b 04 ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/Resource.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.io; import java.io.IOException; import java.io.InputStream; public interface Resource { InputStream getInputStream() throws IOException; } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/UrlResource.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.io; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class UrlResource implements Resource { private final URL url; public UrlResource(URL url) { this.url = url; } @Override public InputStream getInputStream() throws IOException{ URLConnection urlConnection = url.openConnection(); urlConnection.connect(); return urlConnection.getInputStream(); } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/ResourceLoader.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.io; import java.net.URL; public class ResourceLoader { public Resource getResource(String location) { URL resource = this.getClass().getClassLoader().getResource(location); return new UrlResource(resource); } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/BeanDefinitionReader.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; public interface BeanDefinitionReader { void loadBeanDefinitions(String location) throws Exception; } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/AbstractBeanDefinitionReader.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; import java.util.HashMap; import java.util.Map; import net.zhaoxuyang.ioc.bean.io.ResourceLoader; public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader{ private Map<String,BeanDefinition> registry; private ResourceLoader resourceLoader; protected AbstractBeanDefinitionReader(ResourceLoader resourceLoader){ this.registry = new HashMap<>(16); this.resourceLoader = resourceLoader; } public Map<String,BeanDefinition> getRegistry(){ return registry; } public ResourceLoader getResourceLoader(){ return resourceLoader; } } ``` 将`net.zhaoxuyang.ioc.bean` 下的以下三个类,移动到 `net.zhaoxuyang.ioc.bean.factory` 包下: - AbstractBeanFactory - AutowireCapableBeanFactory - BeanFactory 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/xml/XmlBeanDefinitionReader.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.xml; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import net.zhaoxuyang.ioc.bean.AbstractBeanDefinitionReader; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.PropertyValue; import net.zhaoxuyang.ioc.bean.io.ResourceLoader; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { public XmlBeanDefinitionReader(ResourceLoader resourceLoader) { super(resourceLoader); } @Override public void loadBeanDefinitions(String location) throws Exception { InputStream inputStream = getResourceLoader().getResource(location).getInputStream(); doLoadBeanDefinitions(inputStream); } protected void doLoadBeanDefinitions(InputStream inputStream) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(inputStream); // 解析bean registerBeanDefinitions(doc); inputStream.close(); } public void registerBeanDefinitions(Document doc) { Element root = doc.getDocumentElement(); parseBeanDefinitions(root); } protected void parseBeanDefinitions(Element root) { NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; processBeanDefinition(ele); } } } protected void processBeanDefinition(Element ele) { String name = ele.getAttribute("name"); String className = ele.getAttribute("class"); BeanDefinition beanDefinition = new BeanDefinition(); processProperty(ele,beanDefinition); beanDefinition.setBeanClassName(className); getRegistry().put(name, beanDefinition); } 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"); beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name,value)); } } } } ``` 创建文件 `/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.TestService"> <property name="text" value="Hello World!"></property> </bean> </beans> ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/xml/XmlBeanDefinitionReaderTest.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean.xml; import java.util.Map; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.io.ResourceLoader; import org.junit.Assert; import org.junit.Test; public class XmlBeanDefinitionReaderTest { @Test public void testLoadBeanDefinitions() throws Exception { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions("ioc.xml"); Map<String, BeanDefinition> registry = reader.getRegistry(); Assert.assertTrue(registry.size() > 0); } } ``` 在终端输入以下命令: ```shell $ git add . $ git commit -m 'config-beanfactory-with-xml' ```