當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringIOC概述
生活随笔
收集整理的這篇文章主要介紹了
SpringIOC概述
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是SpringIOC
什么是SpringIOC,就是把每一個bean(實體類)與bean(實體了)之間的關系交給第三方容器進行管理。
什么是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 <?xml version="1.0" encoding="UTF-8"?> <beans><bean id="user1" class="com.learn.entity.UserEntity"><property name="userId" value="0001"></property><property name="userName" value="leon"></property></bean><bean id="user2" class="com.learn.entity.UserEntity"><property name="userId" value="0002"></property><property name="userName" value="張三"></property></bean> </beans> package com.learn.entity;public class UserEntity {private String userId;private String userName;public UserEntity(){System.out.println("無參構造函數....");}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;}} package com.learn.service;import java.lang.reflect.Field; import java.util.List;import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader;import com.learn.entity.UserEntity;public class ClassPathXmlApplicationContext {private String xmlPath;public ClassPathXmlApplicationContext(String xmlPath) {this.xmlPath = xmlPath;}public Object getBean(String beanId) throws DocumentException, ClassNotFoundException, NoSuchFieldException,SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException {// spring 加載過程 或者spring ioc實現原理// 1.讀取xml配置文件// 獲取xml解析器SAXReader saxReader = new SAXReader();// this.getClass().getClassLoader().getResourceAsStream("xmlPath")// 獲取當前項目路徑Document read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));// 獲取跟節點對象Element rootElement = read.getRootElement();List<Element> elements = rootElement.elements();Object obj = null;for (Element sonEle : elements) {// 2.獲取到每個bean配置 獲取class地址String sonBeanId = sonEle.attributeValue("id");if (!beanId.equals(sonBeanId)) {continue;}String beanClassPath = sonEle.attributeValue("class");// 3.拿到class地址 進行反射實例化對象 ,使用反射api 為私有屬性賦值Class<?> forName = Class.forName(beanClassPath);obj = forName.newInstance();// 拿到成員屬性List<Element> sonSoneleme = sonEle.elements();for (Element element : sonSoneleme) {String name = element.attributeValue("name");String value = element.attributeValue("value");// 使用反射api 為私有屬性賦值Field declaredField = forName.getDeclaredField(name);// 允許往私有成員賦值declaredField.setAccessible(true);declaredField.set(obj, value);}}// 3.拿到class地址 進行反射實例化對象 ,使用反射api 為私有屬性賦值return obj;}public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException,IllegalArgumentException, IllegalAccessException, InstantiationException, DocumentException {ClassPathXmlApplicationContext appLication = new ClassPathXmlApplicationContext("user.xml");Object bean = appLication.getBean("user1");UserEntity user = (UserEntity) bean;System.out.println(user.getUserId() + "----" + user.getUserName());}} <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="user1" class="com.learn.entity.UserEntity"><property name="userId" value="0002"></property><property name="userName" value="張三"></property></bean><bean id="user2" class="com.learn.entity.UserEntity"><property name="userId" value="0002"></property><property name="userName" value="張三"></property></bean></beans> package com.learn.service;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.entity.UserEntity;@SuppressWarnings("resource") public class Main {public static void main(String[] args) {// 1.讀取springxml配置ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");// 2.獲取bean對象UserEntity userEntity = (UserEntity) classPathXmlApplicationContext.getBean("user1");System.out.println(userEntity.getUserId()+"----"+userEntity.getUserName());}}?
總結
以上是生活随笔為你收集整理的SpringIOC概述的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java反射技术概述
- 下一篇: 数组的定义格式一_动态初始化