spring 底层实现IOC DI简单依赖反射

1.准备实体,dao接口和实现类,service接口和实现类,spring.xml配置文件

实体:public class User implements Serializable{
private Integer id;
private String name;
private Integer age;
public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}

}

dao接口和实现类

public interface UserDao {
List<User> queryAll();
}



public class UserDaoImpl implements UserDao {
@Override
public List<User> queryAll() {
// TODO Auto-generated method stub
System.out.println("++++++query all users++++");
return null;
}
}

service接口和实现类

public interface UserService {
List<User> selectAll();
}

public class UserServiceImpl implements UserService {
     
private UserDao userDao;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}


@Override
public List<User> selectAll() {
// TODO Auto-generated method stub
return userDao.queryAll();
}
}

spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
<bean id="userDao" class="org.hi.software.test.dao.UserDaoImpl"/>
<bean id="userService" class="org.hi.software.test.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>

2.

a、读取xml文件

//存放所有的bean节点
HashMap<String, String> beanMap = new HashMap<String, String>();
//存放所有bean节点对应的property集合{'beanid1':'{'name':'ref','name','value'}','beanid2':'{'',''}'}
HashMap<String, Map<String, String>> propertyMap = new HashMap<String, Map<String, String>>();
//工厂存放beanid ,和对应的对象
HashMap<String, Object> context = new HashMap<String, Object>();

//读取xml文件拿到id和class,放入工厂beanMap
DocumentBuilderFactory newInstance = DocumentBuilderFactory . newInstance ();
//读取配置文件
            Document document = newInstance .newDocumentBuilder().parse( "src/org/hi/software/spring.xml" );
             //通过节点名称获取所有的节点集合
             NodeList elementsByTagName = document .getElementsByTagName( "bean" );

b、

拿到bean节点,遍历所有的property属性拿到property属性的集合propertyAttributeMap(<name,ref>或者<name,value>)
propertyAttributeMap作为value,beanId作为key,放入集合propertyMap
//遍历拿到节点的key value
             for ( int i = 0; i < elementsByTagName .getLength(); i ++) {
                Element beanitem = (Element) elementsByTagName .item( i );
                String beanid = beanitem .getAttribute( "id" );
                String beanclass = beanitem .getAttribute( "class" );
                 //System.out.println( beanclass +"---->"+ beanid );
                 beanMap .put( beanid , beanclass );
                 //获取每个bean下的property属性
                    NodeList elementsByTagName2 = beanitem .getElementsByTagName( "property" );
                    Map<String, String> propertyAttributeMap = new HashMap<String, String>();
                         for ( int j = 0; j < elementsByTagName2 .getLength(); j ++) {
                            Element propertyitem = (Element) elementsByTagName2 .item( j );
                            String name = propertyitem .getAttribute( "name" );
                            String ref = propertyitem .getAttribute( "ref" );
                             //System.out.println(name+"---"+ ref );
                             propertyAttributeMap .put( name , ref );
                        }
                         propertyMap .put( beanid , propertyAttributeMap );
            }
d、IOC:beanMap转化为context工厂(beanID-->对象)
for  (Map.Entry<String, String>  bean  :  beanMap .entrySet()) {
                String  beanid  =  bean .getKey();
                String  beanclass  =  bean .getValue();
                Object  object  = Class. forName ( beanclass ).newInstance();
                 context .put( beanid ,  object );
                
            }
e、DI:遍历propertyMap,通过key找到bean对象,value就是property集合包含name和value(ref)
        组装setName方法进行赋值,如果不是基本类型,应通过context获得复杂对象进行赋值
for  (Map.Entry<String, Map<String, String>>  beanProperty : propertyMap .entrySet()) {
                String  bid  =  beanProperty .getKey();
                Object  target  =  context .get( bid );
                System. out .println( "target:" + target );
                Map<String, String>  value  =  beanProperty .getValue();
                 for  (Map.Entry<String, String>  proString  : value .entrySet()) {
                    String  name  =  proString .getKey();
                    String  ref  =  proString .getValue();
                    Object  object  =  context .get( ref );
                    System. out .println( "object:" + object );
                    String  methodName = "set" + name .substring(0, 1).toUpperCase()+ name .substring(1);
                    Method  declaredMethod  =  target .getClass().getDeclaredMethod( methodName , context .get( ref ).getClass().getInterfaces()[0]);
                     declaredMethod .invoke( target ,  object );
                }
3.测试
UserService us = (UserService) context.get("userService");
us.selectAll();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值