多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
```shell $ git checkout -b 02 # 检出新分支 切换到一个新分支 '02' ``` 修改 `BeanDefinition.java`,修改后的内容为: ```java package net.zhaoxuyang.ioc.bean; public class BeanDefinition { private Object bean; private Class beanClass; private String beanClassName; public BeanDefinition(){} public BeanDefinition(Object bean) { this.bean = bean; } public Object getBean() { return bean; } public void setBean(Object bean) { this.bean = bean; } public Class getBeanClass() { return beanClass; } public void setBeanClass(Class beanClass) { this.beanClass = beanClass; } public String getBeanClassName() { return beanClassName; } public void setBeanClassName(String beanClassName) { this.beanClassName = beanClassName; try{ this.beanClass = Class.forName(beanClassName); } catch (ClassNotFoundException ex) { System.out.println(ex); } } } ``` 删除文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/BeanFactory.java` ,新增三个文件: - `BeanFactory.java` - `AbstractBeanFactory.java` - `AutowrieCapableBeanFactory.java` `BeanFactory.java` 的内容为: ```java package net.zhaoxuyang.ioc.bean; public interface BeanFactory { public Object getBean(String name); public void registerBeanDefinition(String name, BeanDefinition beanDefinition); } ``` `AbstractBeanFactory.java` 的内容为: ```java package net.zhaoxuyang.ioc.bean; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public abstract class AbstractBeanFactory implements BeanFactory{ private Map<String, BeanDefinition> beanDefinitionMap; public AbstractBeanFactory(){ beanDefinitionMap = new ConcurrentHashMap<>(16); } @Override public Object getBean(String name) { return beanDefinitionMap.get(name).getBean(); } @Override public void registerBeanDefinition(String name, BeanDefinition beanDefinition) { Object bean = doCreateBean(beanDefinition); beanDefinition.setBean(bean); beanDefinitionMap.put(name, beanDefinition); } protected abstract Object doCreateBean(BeanDefinition beanDefinition); } ``` `AutowrieCapableBeanFactory.java` 的内容为: ```java package net.zhaoxuyang.ioc.bean; public class AutowireCapableBeanFactory extends AbstractBeanFactory { @Override protected Object doCreateBean(BeanDefinition beanDefinition) { Object bean = null; try { beanDefinition.getBeanClass().newInstance(); } catch (InstantiationException | IllegalAccessException ex) { System.out.println(ex); } return bean; } } ``` 新增测试服务 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/TestService.java` ,内容为: ```java package net.zhaoxuyang.ioc.bean; public class TestService { public void echo(){ System.out.println("Hello"); } } ``` 修改测试文件 `BeanFactoryTest.java` ,内容为: ```java package net.zhaoxuyang.ioc.bean; import org.junit.Test; public class BeanFactoryTest { @Test public void testGetBean() { // [1] 初始化beanFactory BeanFactory beanFactory = new AutowireCapableBeanFactory(); // [2] 注入bean BeanDefinition beanDefinition = new BeanDefinition(); beanDefinition.setBeanClassName("net.zhaoxuyang.ioc.bean.TestService"); beanFactory.registerBeanDefinition("testService", beanDefinition); // [3] 获取bean TestService service = (TestService) beanFactory.getBean("testService"); service.echo(); } } ``` 在终端输入以下命令: ```shell $ git add . $ git commit -m '02' ```