SpringIoc原理分析(手把手教你自制springioc框架)

5 篇文章 0 订阅
2 篇文章 0 订阅

手写SpringIOC框架

什么是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.读取bean的XML配置文件
  • 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代码

public class ClassPathXmlApplicationContext {
	private String pathXml = null;

	public ClassPathXmlApplicationContext(String pathXml) {
		this.pathXml = pathXml;
	}

	public Object getBean(String beanId) throws Exception {
		if (StringUtils.isEmpty(beanId)) {
			throw new Exception("beanId is null");
		}
		SAXReader saxReader = new SAXReader();
		Document read = saxReader.read(this.getClass().getClassLoader().getResource(pathXml));
		// 获取到根节点
		Element rootElement = read.getRootElement();
		// 根节点下所有的子节点
		List<Element> elements = rootElement.elements();
		for (Element element : elements) {
			// 获取到节点上的属性
			String id = element.attributeValue("id");
			if (StringUtils.isEmpty(id)) {
				continue;
			}
			if (!id.equals(beanId)) {
				continue;
			}

			// 使用java反射机制初始化对象
			String beanClass = element.attributeValue("class");
			Class<?> forName = Class.forName(beanClass);
			Object newInstance = forName.newInstance();
			List<Element> propertyElementList = element.elements();
			for (Element el : propertyElementList) {
				String name = el.attributeValue("name");
				String value = el.attributeValue("value");
				Field declaredField = forName.getDeclaredField(name);
				declaredField.setAccessible(true);
				declaredField.set(newInstance, value);
			}
			return newInstance;

		

		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext classPath = new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) classPath.getBean("user2");
		System.out.println(user.getUserId() + "---" + user.getUserName());
	}
}

Maven坐标

<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.4</version>
		</dependency>
		<dependency>
			<groupId>org.dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>2.0.0</version>
		</dependency>
	</dependencies>
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值