企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在终端执行以下命令: ```shell git checkout -b 06 ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/context/ApplicationContext.java` ,其内容为: ```java package net.zhaoxuyang.ioc.context; import net.zhaoxuyang.ioc.bean.factory.BeanFactory; public interface ApplicationContext extends BeanFactory { } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/context/AbstractApplicationContext.java` ,其内容为: ```java package net.zhaoxuyang.ioc.context; import net.zhaoxuyang.ioc.bean.BeanDefinition; import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory; public abstract class AbstractApplicationContext implements ApplicationContext { protected AbstractBeanFactory beanFactory; public AbstractApplicationContext(AbstractBeanFactory beanFactory){ this.beanFactory = beanFactory; } public abstract void refresh() throws Exception; @Override public Object getBean(String name) throws Exception { return beanFactory.getBean(name); } @Override public void registerBeanDefinition(String name, BeanDefinition bd) throws Exception { throw new UnsupportedOperationException("Not supported yet."); } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/context/ClassPathXmlApplicationContext.java` ,其内容为: ```java package net.zhaoxuyang.ioc.context; import java.util.Map; import net.zhaoxuyang.ioc.bean.BeanDefinition; 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; public class ClassPathXmlApplicationContext extends AbstractApplicationContext{ private String configLocation; public ClassPathXmlApplicationContext(String configLocation) throws Exception{ this(configLocation, new AutowireCapableBeanFactory()); } public ClassPathXmlApplicationContext(String configLocation,AbstractBeanFactory beanFactory) throws Exception{ super(beanFactory); this.configLocation = configLocation; refresh(); } @Override public void refresh() throws Exception{ XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader()); reader.loadBeanDefinitions(configLocation); for(Map.Entry<String,BeanDefinition> entry:reader.getRegistry().entrySet()){ beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); } } } ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/context/ApplicationContextTest.java` ,其内容为: ```java package net.zhaoxuyang.ioc.context; import net.zhaoxuyang.ioc.bean.TestService; import org.junit.Test; public class ApplicationContextTest { @Test public void test() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ioc.xml"); TestService testService = (TestService) applicationContext.getBean("testService"); testService.echo(); } } ``` 在终端执行以下命令: ```shell $ git add . $ git commit -m 'invite-application-context' ```