spring的ioc机制

(一)首先定义一个类

public class JavaBean {
	private String userName;

	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	
}


(二)定义服务类,读取配置文件并且利用java的反射机制构造对象的参数以及方法

public class BeanFactory {
	private Map<String, Object> beanMap = new HashMap<String, Object>();

	/** */
	/**
	 * bean工厂的初始化.
	 * 
	 * @param xml
	 *            xml配置文件
	 */
	public void init(String xml) {
		try {
			// 读取指定的配置文件
			SAXReader reader = new SAXReader();
			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();
			// 从class目录下获取指定的xml文件
			InputStream ins = classLoader.getResourceAsStream(xml);
			Document doc = reader.read(ins);
			Element root = doc.getRootElement();
			Element foo;
			// 遍历bean
			for (Iterator i = root.elementIterator("bean"); i.hasNext();) {
				foo = (Element) i.next();
				// 获取bean的属性id和class
				Attribute id = foo.attribute("id");
				Attribute cls = foo.attribute("class");
				// 利用Java反射机制,通过class的名称获取Class对象
				Class bean = Class.forName(cls.getText());
				// 获取对应class的信息
				java.beans.BeanInfo info = java.beans.Introspector
						.getBeanInfo(bean);
				// 获取其属性描述
				java.beans.PropertyDescriptor pd[] = info
						.getPropertyDescriptors();
				// 设置值的方法
				Method mSet = null;
				// 创建一个对象
				Object obj = bean.newInstance();
				// 遍历该bean的property属性
				for (Iterator ite = foo.elementIterator("property"); ite
						.hasNext();) {
					Element foo2 = (Element) ite.next();
					// 获取该property的name属性
					Attribute name = foo2.attribute("name");
					String value = null;
					// 获取该property的子元素value的值
					for (Iterator ite1 = foo2.elementIterator("value"); ite1
							.hasNext();) {
						Element node = (Element) ite1.next();
						value = node.getText();
						break;
					}
					for (int k = 0; k < pd.length; k++) {
						if (pd[k].getName().equalsIgnoreCase(name.getText())) {
							mSet = pd[k].getWriteMethod();
							// 利用Java的反射极致调用对象的某个set方法,并将值设置进去 mSet.invoke(obj,
							// value);
							mSet.invoke(obj, value);
						}
					}
				}

				// 将对象放入beanMap中,其中key为id值,value为对象
				beanMap.put(id.getText(), obj);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	/** */
	/**
	 * 通过bean的id获取bean的对象.
	 * 
	 * @param beanName
	 *            bean的id
	 * @return 返回对应对象
	 */
	public Object getBean(String beanName) {
		Object obj = beanMap.get(beanName);
		return obj;
	}

	/** */
	/**
	 * 测试方法.
	 * 
	 * @param args
	 * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a> Creation
	 *         date: 2007-10-6 - 上午11:21:14
	 */
	public static void main(String[] args) {
		BeanFactory factory = new BeanFactory();
		factory.init("config.xml");
		JavaBean javaBean = (JavaBean) factory.getBean("javaBean");
		System.out.println("userName=" + javaBean.getUserName());
		System.out.println("password=" + javaBean.getPassword());
	}
}

(三)配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="javaBean" class="org.springioc.reflection.JavaBean">
       <property name="userName">
           <value>水墨夜色</value>
       </property>
       <property name="password">
           <value>12345678</value>
       </property>
    </bean>
</beans>

以上就是spring的Ioc机制的简单实现。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值