IOC
- 控制翻转,它是一种设计思想,而并非实际的技术,最核心的思想就是预先将设计的对象实例创建的控制权交给程序(IOC容器)
- IOC容器的本质是一个K-V结构的map
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建方式1:空参构造创建 -->
<bean id="a" class="com.liulin.demo6.A"/>
<!--
创建方式2:静态工厂创建
调用A的createBObj方法来创建名为b的对象放入容器
-->
<bean id="b" class="com.liulin.demo6.A" factory-method="createBObj"/>
<!--
创建方式3:实例工厂创建
调用实例a的createCObj方法来创建名为c的对象放入容器
-->
<bean id="c" factory-bean="a" factory-method="createCObj"/>
</beans>
代码
public class A {
public A() {
System.out.println("A 无参构造器被调用了.");
}
public static B createBObj() {
System.out.println("A 的静态方法 createBObj 被调用了.");
return new B();
}
public C createCObj() {
System.out.println("A 的实例方法 createCObj 被调用了.");
return new C();
}
}
BeanConfig类
public class BeanConfig {
private String id;
private String clazz;
private String factoryMethod;
private String factoryBean;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getFactoryMethod() {
return factoryMethod;
}
public void setFactoryMethod(String factoryMethod) {
this.factoryMethod = factoryMethod;
}
public String getFactoryBean() {
return factoryBean;
}
public void setFactoryBean(String factoryBean) {
this.factoryBean = factoryBean;
}
}
Init类
public class Init {
public static void main(String[] args) throws Exception {
// 模拟解析xml 拿到 beanConfigs
List<BeanConfig> beanConfigs = parseXmltoBeanConfig();
//循环
for (BeanConfig tmpconfig : beanConfigs) {
if (null != tmpconfig.getClazz()) {
//拿到clazz
Class clazz = Class.forName(tmpconfig.getClazz());
if (null != tmpconfig.getFactoryMethod())
{
//基于Class对象获取method对象
Method method = clazz.getDeclaredMethod(tmpconfig.getFactoryMethod());
IOCContainer.putBean(tmpconfig.getId(), method.invoke(null));
} else { //构造器创建bean实例
IOCContainer.putBean(tmpconfig.getId(), clazz.newInstance());
}
} else if(null != tmpconfig.getFactoryBean()) {
//从容器中拿到实体bean
Object obj = IOCContainer.getBean( tmpconfig.getFactoryBean());
Method method = obj.getClass().getDeclaredMethod(tmpconfig.getFactoryMethod());
IOCContainer.putBean(tmpconfig.getId(), method.invoke(obj));
}else{
System.out.println("不知道在搞什么鬼.2个核心的配置都没有!");
}
}
}
/**
* 模拟一个解析XML过程
*
* @return
*/
private static List<BeanConfig> parseXmltoBeanConfig() {
List<BeanConfig> beanConfigs = new ArrayList<BeanConfig>();
BeanConfig beanConfig1 = new BeanConfig();
beanConfig1.setClazz("com.liulin.reflect.demo6.A");
beanConfig1.setId("a");
beanConfigs.add(beanConfig1);
BeanConfig beanConfig2 = new BeanConfig();
beanConfig2.setClazz("com.liulin.reflect.demo6.A");
beanConfig2.setId("b");
beanConfig2.setFactoryMethod("createBObj");
beanConfigs.add(beanConfig2);
BeanConfig beanConfig3 = new BeanConfig();
beanConfig3.setId("c");
beanConfig3.setFactoryBean("a");
beanConfig3.setFactoryMethod("createCObj");
beanConfigs.add(beanConfig3);
return beanConfigs;
}
}
IOCContainer类
public class IOCContainer {
private static HashMap container = new HashMap();
public static void putBean(String id,Object object){
container.put(id,object);
}
public static Object getBean(String id){
return container.get(id);
}
}