springIOC

springIOC控制反转:将每个bean于bean之间的关系交给第三方容器spring进行管理
springIOC原理:dom4j+java的反射机制
1.解析xml
2.使用bean id查找对应的xml节点,获取class节点属性
3.使用java的反射机制初始化类
4.使用java反射机制给私有属性赋值

反射机制缺点:初始化对象效率低,耗资源
优点:扩展性高
例子:
applicationContext.xml




    
        
        
    
    
        
        
    


   

UserEntity.java

package com.crj.entity;

/**   
 * @author: crj
 * @date: 2018年12月5日 下午3:36:18 
 */
public class UserEntity {
    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;
    }


    @Override
    public String toString() {
        return "UserEntity [userId=" + userId + ", userName=" + userName + "]";
    }
    
    
    
    

}

ClassPathXmlApplicationContext.java

package com.crj;

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

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

import com.crj.entity.UserEntity;

/**   
 * @author: crj
 * @date: 2018年12月5日 下午3:57:25 
 */
public class ClassPathXmlApplicationContext {
    private String PATH;
    private String ID;
    private String CLASS;
    private String NAME;
    private String VALUE;
    public void init() {
        ID = "id";
        CLASS ="class";
        NAME = "name";
        VALUE = "value";
    }
    public ClassPathXmlApplicationContext(String path) {
        this.PATH = path;
    }
    public Object getBean(String beanId) throws DocumentException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException {
        init();
        //      1.解析xml
        if(StringUtils.isEmpty(beanId)) {
            return null;
        }
        
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read(this.getClass().getClassLoader().getResource(PATH));
        Element rootElement = read.getRootElement();
        List elements = rootElement.elements();
        for (Element element : elements) {
            String id = element.attributeValue(ID);
            if(!beanId.equals(id)) {
                continue;//结束本次循环
            }
//          2.使用bean id查找对应的xml节点,获取class节点属性
            String attClass = element.attributeValue(CLASS);
//          3.使用java的反射机制初始化类
            Class forName = Class.forName(attClass);
            Object newInstance = forName.newInstance();
            List elements2 = element.elements();
            for (Element element2 : elements2) {
                String attField = element2.attributeValue(NAME);
                String attValue = element2.attributeValue(VALUE);
                Field declaredField = forName.getDeclaredField(attField);
                declaredField.setAccessible(true);
//              4.使用java反射机制给私有属性赋值
                declaredField.set(newInstance, attValue);
            }
            return newInstance;
            
        }
        return null;
    }
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException, DocumentException {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserEntity user = (UserEntity) app.getBean("user1");
        System.out.println(user.toString());
    }

}

你可能感兴趣的:(springIOC)