利用反射手写springioc

package com.it.java;


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.it.entity.UserEntity;


/**
 * 利用反射手写springioc 
 * @author proxy
 *
 */
public class ClassPathXmlApplicationContext {
private String  xmlPath;


public ClassPathXmlApplicationContext(String xmlPath) {
super();
this.xmlPath = xmlPath;
}

public Object getBean(String beanId) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{

//一.读取xml配置文件
SAXReader saxReader = new SAXReader();
//1.获取当前项目路径
Document read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
//2.获取根节点对象
Element rootElement = read.getRootElement();
List<Element> elements = rootElement.elements();
Object obj =null;
for (Element sonEle : elements) {
//二.获取道每个Bean配置,获取class地址
String sonBeanId =sonEle.attributeValue("id");
if(!beanId.equals(sonBeanId)){
continue;
}
String beanClassPath = sonEle.attributeValue("class");
Class<?> forName = Class.forName(beanClassPath);
obj=forName.newInstance();
//4。拿到成员属性
List<Element> sonSoneleme = sonEle.elements();
for (Element element : sonSoneleme) {
String name =element.attributeValue("name");
String value =element.attributeValue("value");
//三.拿到class地址 进行反射实例化对象,使用反射api为私有属性赋值
Field declaredField = forName.getDeclaredField(name);
//运行往私有成员赋值
declaredField.setAccessible(true);
declaredField.set(obj, value);
}
}
//3.拿到class 地址    进行反射实力化对象
return obj;
}

public static void main(String[] args) throws 
DocumentException, ClassNotFoundException, InstantiationException, 
IllegalAccessException, NoSuchFieldException, SecurityException {
//读取xml
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("user.xml");
//读取beanid
Object bean = classPathXmlApplicationContext.getBean("user1");
//拿到实体类
UserEntity user = (UserEntity) bean;
System.out.println(user.getUserId()+"-----"+user.getUserName());
}

}




-------------------------------------------------------------------------------------------------------------------------------
package com.it.entity;
public class UserEntity {
private String userId;
private String userName;
public UserEntity(){
System.out.println("使用反射技术 ,执行无参数构造 函数");
}
public UserEntity(String userId,String userName) {
 System.out.println("使用反射技术 执行 有参构造函数 userId:"+userId+"----userName:"+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;
}


}
-------------------------------------------------------------------------------------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="user1" class="com.it.entity.UserEntity">
<property name="userId" value="0001"></property>
<property name="userName" value="李四"></property>
</bean>
<bean id="user2" class="com.it.entity.UserEntity">
<property name="userId" value="0002"></property>
<property name="userName" value="张三"></property>
</bean>
</beans>
-------------------------------------------------------------------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值