手动编写SpringIOC框架
  TEZNKK3IfmPf 2023年11月13日 44 0

一.什么是SpringIOC

什么是SpringIOC,就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理。

Xml配置:

<beans>
	<bean id="user1" class="com.itmayiedu.entity.UserEntity">
		<property name="userId" value="0001"></property>
		<property name="userName" value="余胜军"></property>
	</bean>
	<bean id="user2" class="com.itmayiedu.entity.UserEntity">
		<property name="userId" value="0002"></property>
		<property name="userName" value="张三"></property>
	</bean>
</beans>

Java代码:

//1.读取springxml配置
		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		//2.获取bean对象
		TestService testService = (TestService) classPathXmlApplicationContext.getBean("testService");
		System.out.println(testService.test());

二.什么是SpringIOC底层实现原理

1.读取beanXML配置文件

2.使用beanId查找bean配置,并获取配置文件中class地址。

3.使用Java反射技术实例化对象

4.获取属性配置,使用反射技术进行赋值。

详细步骤:

(1).利用传入的参数获取xml文件的流,并且利用dom4j解析成Document对象

2).对于Document对象获取根元素对象<beans>后对下面的<bean>标签进行遍历,判断是否有符合的id.

3).如果找到对应的id,相当于找到了一个Element元素,开始创建对象,先获取class属性,根据属性值利用反射建立对象.

4).遍历<bean>标签下的property标签,并对属性赋值.注意,需要单独处理int,float类型的属性.因为在xml配置中这些属性都是以字符串的形式来配置的,因此需要额外处理.

5).如果属性property标签有ref属性,说明某个属性的值是一个对象,那么根据id(ref属性的值)去获取ref对应的对象,再给属性赋值.

6.返回建立的对象,如果没有对应的id,或者<beans>下没有子标签都会返回null

三.建立实体类:

public class User {

	private String userId;
	private String userName;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
}

四.使用反射技术完成Java代码(这就是springioc的底层代码):

import java.lang.reflect.Field;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.itmayiedu.entity.User;

/**
 * 
 * @classDesc: 功能描述:(读取Spring配置文件)
 * @author: 余胜军
 * @createTime: 2017年8月25日 上午1:27:55
 * @version: v1.0
 * @copyright:上海每特教育科技有限公司
 */
public class ClassPathXmlApplicationContext {
	private String xmlPath;

	/**
	 * 
	 * @param xmlPath
	 *            spring xml 配置路径
	 */
	public ClassPathXmlApplicationContext(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public Object getBean(String beanId) throws Exception {

		// 解析xml器
		SAXReader saxReader = new SAXReader();
		Document read = null;
		try {
			// 从项目根目录路径下 读取
			read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (read == null) {
			return null;
		}
		// 获取根节点资源
		Element root = read.getRootElement();
		List<Element> elements = root.elements();
		if (elements.size() <= 0) {
			return null;
		}
		Object oj = null;
		for (Element element : elements) {
			String id = element.attributeValue("id");
			if (StringUtils.isEmpty(id)) {
				return null;
			}
			if (!id.equals(beanId)) {
				continue;
				// throw new Exception("使用beanId:" + beanId + ",未找到该bean");
			}
			// 获取实体bean class地址
			String beanClass = element.attributeValue("class");
			// 使用反射实例化bean
			Class<?> forNameClass = Class.forName(beanClass);
			oj = forNameClass.newInstance();
			// 获取子类对象
			List<Element> attributes = element.elements();
			if (attributes.size() <= 0) {
				return null;
			}
			for (Element et : attributes) {
				// 使用反射技术为方法赋值
				String name = et.attributeValue("name");
				String value = et.attributeValue("value");
				Field field = forNameClass.getDeclaredField(name);
				field.setAccessible(true);
				field.set(oj, value);

			}

		}
		return oj;
		// 1.使用beanId查找配置文件中的bean。
		// 2.获取对应bean中的classpath配置
		// 3.使用java反射机制实体化对象
	}

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		User bean = (User) applicationContext.getBean("user2");
		System.out.println("使用反射获取bean" + bean.getUserId() + "---" + bean.getUserName());

	}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   21   0   0 Beanxmljava
  TEZNKK3IfmPf   2023年11月12日   83   0   0 Beanspring
  TEZNKK3IfmPf   2023年11月13日   45   0   0 Bean
  TEZNKK3IfmPf   2023年11月13日   44   0   0 Beanspring
  TEZNKK3IfmPf   2023年11月13日   35   0   0 Bean容器
TEZNKK3IfmPf