springIOC的简单实现

主要内容参考 http://www.cnblogs.com/fingerboy/p/5425813.html

Ioc—Inversion of Control,即控制反转,不是什么技术,而是一种设计思想。在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。即Spring管理对象生命周期及依赖关系



程序中所有的Bean之间的依赖关系我们是放在一个xml文件中进行维护的,就是applicationContext.xml

ConfigManager类完成的功能是读取xml,并将所有读取到有用的信息封装到我们创建的一个Map<String,Bean>集合中,用来在初始化容器时创建bean对象.

定义一个BeanFactory的接口,接口中有一个getBean(String name)方法,用来返回你想要创建的那个对象.

然后定义一个该接口的实现类ClassPathXmlApplicationContext.就是在这个类的构造方法中,初始化容器,通过调用ConfigManager的方法返回的Map集合,通过反射一一创建bean对象.这里需要注意,对象的创建有两个时间点,这取决与bean标签中scope属性的值:

如果scope="singleton",那么对象在容器初始化时就已创建好,用的时候只需要去容器中取即可.

如果scope="prototype",那么容器中不保存这个bean的实例对象,每次开发者需要使用这个对象时再进行创建.


实体类 Student Teacher

package com.entity;

public class Teacher {
	private String name;
	private Student student;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

	public String toString() {
		return "Teacher [name=" + name + ", student=" + student + "]";
	}
	public Teacher(String name, Student student) {
		super();
		this.name = name;
		this.student = student;
	}
	public Teacher() {
		super();
	}
	
}


package com.entity;
public class Student {
	private String name;
	private Integer age;
	private String address;	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", address="
				+ address + "]";
	}
}

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<!-- <bean id="str" class="com.lang.String"></bean> -->

	<bean id="teacher" class="com.entity.Teacher"  scope="prototype">
		<property name="name" value="肖肖"></property>
		<property name="student" ref="student"></property>
	</bean>
	
	<bean id="student" class="com.entity.Student">
		<property name="name" value="小明"></property>
		<property name="age" value="1"></property>
		<property name="address" value="陕西临潼"></property>
	</bean>
</beans>

用于封装Bean标签的类 Bean

package com.config;
import java.util.ArrayList;
import java.util.List;

public class Bean {
	private String name;
	private String className;
	private String scope="singleton";
	private List<Property> properties=new ArrayList<Property>();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getScope() {
		return scope;
	}
	public void setScope(String scope) {
		this.scope = scope;
	}
	public List<Property> getProperties() {
		return properties;
	}
	public void setProperties(List<Property> properties) {
		this.properties = properties;
	}
}

用于封装Bean子标签 Property

package com.config;
public class Property {
	private String name;
	private String value;
	private String ref;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getRef() {
		return ref;
	}
	public void setRef(String ref) {
		this.ref = ref;
	}

	public String toString() {
		return "Property [name=" + name + ", value=" + value + ", ref=" + ref
				+ "]";
	}
}

解析xml文件的ConfigManager

package com.config.parse;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.config.Bean;
import com.config.Property;

public class ConfigManager {
	private static Map<String,Bean> map=new HashMap<String,Bean>();
	public static Map<String,Bean> getConfig(String path){
		//xml解析器
		SAXReader reader=new SAXReader();
		
		InputStream in=ConfigManager.class.getResourceAsStream(path);
		
		Document doc=null;
		try {
			doc=reader.read(in);
		} catch (DocumentException e) {
			e.printStackTrace();
			System.out.println("初始化出错");
		}
		
		//从任意节点的位置查找bean节点
		String xpath="//bean";
		
		List<Element> beans=doc.selectNodes(xpath);
		
		if(beans!=null){
			for(Element beanElement:beans){
				Bean bean=new Bean();
				String name=beanElement.attributeValue("id");
				String className=beanElement.attributeValue("class");
				String scope=beanElement.attributeValue("scope");
				bean.setName(name);
				bean.setClassName(className);
				if(scope!=null){
					bean.setScope(scope);
				}
				List<Element> properties=beanElement.elements("property");
				if(properties!=null){
					for(Element propertyElement:properties){
						Property property=new Property();
						String pname=propertyElement.attributeValue("name");
						String pvalue=propertyElement.attributeValue("value");
						String pref=propertyElement.attributeValue("ref");
						
						property.setName(pname);
						property.setValue(pvalue);
						property.setRef(pref);
						
						bean.getProperties().add(property);
					}
				}
				map.put(name, bean);
			}
		}
		return map;
	}
}

注入属性的工具类 InjectUtil

package com.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InjectUtil {
	//注入value
	public static void injectValue(Object obj,String name,String value){
		Method[] methods=obj.getClass().getMethods();
		String methodName="set"+name.substring(0, 1).toUpperCase()+name.substring(1);
		
		for(Method method:methods){
			if(method.getName().equals(methodName)){
				Class<?>[] parameters=method.getParameterTypes();
				Class<?> parameter=parameters[0];
					
				try {
					Object paraObj=parameter.getConstructor(String.class).newInstance(value);
					method.invoke(obj, paraObj);
				} catch (InstantiationException | IllegalAccessException
						| IllegalArgumentException
						| InvocationTargetException | NoSuchMethodException
						| SecurityException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//注入ref
	public static void injectRef(Object obj,String name,Object ref){
		
		Method[] methods=obj.getClass().getMethods();
		String methodName="set"+name.substring(0,1).toUpperCase()+name.substring(1);

		
		for(Method method:methods){

			if(method.getName().equals(methodName)){
				try {
					method.invoke(obj, ref);
				} catch (IllegalAccessException | IllegalArgumentException
						| InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

BeanFacroty接口

package com.main;

public interface BeanFactory {
	public Object getBean(String name);
}

ApplicationContext

package com.main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.config.Bean;
import com.config.Property;
import com.config.parse.ConfigManager;
import com.utils.InjectUtil;

public class ClassPathXmlApplicationContext implements BeanFactory{
	//读取配置文件的map
	private Map<String,Bean> configMap;
	
	private Map<String,Object> context=new HashMap<String,Object>();
	
	public ClassPathXmlApplicationContext(String path){
		configMap=ConfigManager.getConfig(path);
		
		for(Entry<String,Bean> entry:configMap.entrySet()){
			String name=entry.getKey();
			Bean bean=entry.getValue();
			
			Object obj=context.get(name);
			if(obj==null&&bean.getScope().equals("singleton")){
				obj=createBean(bean);
				context.put(name, obj);
			}
		}
	}
	
	public Object createBean(Bean bean){
		String className=bean.getClassName();
		Class<?> c=null;
		try {
			c=Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		Object obj=null;
		try {
			obj=c.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		
		List<Property> properties=bean.getProperties();
		if(properties!=null){
			for(Property property:properties){
				String name=property.getName();
				String value=property.getValue();
				String ref=property.getRef();
				
				if(value!=null){
					InjectUtil.injectValue(obj, name,value);
				}
				
				if(ref!=null){
					Object propertyObj=context.get(ref);
					if(context.get(ref)==null){
						propertyObj=createBean(configMap.get(ref));
						if(configMap.get(ref).getScope().equals("singleton")){
							context.put(ref,propertyObj);
						}
					}
					InjectUtil.injectRef(obj,ref,propertyObj);
				}
			}
		}
		
		return obj;
	}
	
	public Object getBean(String name){
		Bean bean=configMap.get(name);
		if(bean.getScope().equals("singleton")){
			return context.get(name);
		}else{
			return createBean(bean);
		}
	}
}

测试类

package com.test;

import com.entity.Student;
import com.entity.Teacher;
import com.main.BeanFactory;
import com.main.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args){
		BeanFactory context=
				new ClassPathXmlApplicationContext("/ApplicationContext.xml");
		Student student1=(Student)context.getBean("student");
		Student student2=(Student)context.getBean("student");
		System.out.println(student1);
		System.out.println(student1==student2);
		
		Teacher teacher1=(Teacher)context.getBean("teacher");
		Teacher teacher2=(Teacher)context.getBean("teacher");
		System.out.println(teacher1);
		System.out.println(teacher1==teacher2);
		
		String s = "";
	}
}

测试结果

Student [name=小明, age=1, address=陕西临潼]
true
Teacher [name=肖肖, student=Student [name=小明, age=1, address=陕西临潼]]
false

只有单例模式容器才会持有该对象的引用,若是原型模式只有客户端持有该对象的引用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值