自定义IOC 简单实现
IOC概念:
IOC(inversion of control)的中文解释是“控制反转”,对象的使用者不是创建者. 作用是将对象的创建 反转给spring框架来创建和管理。说白了就是把对象交给spring创建和管理. 我们要用对象时直接从Spring的IOC容器【集合】中获取,使用spring最主要的目的是实现解耦, 到时候我们只需要修改配置文件, 不需要修改源码
简单实现流程
1.创建maven工程
2.导入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 指定jdk,防止update project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 项目编码-->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
3.代码实现
3.1 获取bean的工厂编写
package com.lyf.factory;
import com.lyf.util.Resources;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyBeanFactory {
/**
* 存放bean的容器
*/
private static Map<String, Object> map;
// 加载核心配置文件到流中
static {
InputStream is = null;
try {
map = new HashMap<>();
is = Resources.getResourceAsStream("applicationContext.xml");
// 用于解析xml文件的一个类, 第三方的
SAXReader saxReader = new SAXReader();
// 把inputStream读取到SAXReader类中
Document document = saxReader.read(is);
// 获取所有的节点对象 //bean
List<Element> list = document.selectNodes("//bean");
for (Element element : list) {
String id = element.attributeValue("id");
String className = element.attributeValue("class");
Object obj = Class.forName(className).newInstance();
map.put(id, obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 返回一个Object对象
*
* @return java.lang.Object
*/
public static Object getBean(String id) {
return map.get(id);
}
}
3.2 service层
package com.lyf.service;
import com.lyf.bean.Account;
public interface AccountService {
/**
* 保存一个账户
*
* @param account 账户
*/
void save(Account account);
3.3 service层接口实现类
package com.lyf.service.impl;
import com.lyf.bean.Account;
import com.lyf.service.AccountService;
public class AccountServiceImpl implements AccountService {
/**
* 保存一个账户
*
* @param account 账户
*/
@Override
public void save(Account account) {
System.out.println("AccountServiceImpl 执行了保存一个账户方法..." + account);
}
}
3.4 bean
package com.lyf.bean;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String username;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", money=" + money +
'}';
}
}
3.5 工具类
package com.lyf.util;
import java.io.InputStream;
public class Resources {
/**
* 获取inputStream流的工具类
*
* @param classPath 配置文件所在地址
* @return java.io.InputStream
*/
public static InputStream getResourceAsStream(String classPath) {
return Resources.class.getClassLoader().getResourceAsStream(classPath);
}
}
3.6 测试类
package com.lyf.test;
import com.lyf.bean.Account;
import com.lyf.factory.MyBeanFactory;
import com.lyf.service.AccountService;
import org.junit.Test;
public class CustomIOCTest {
@Test
public void testCustomIOC() throws Exception {
AccountService accountService = (AccountService) MyBeanFactory.getBean("accountService");
Account account = new Account();
account.setId(1);
account.setUsername("张三");
account.setMoney(1000.0);
accountService.save(account);
}
}