Spring学习 2

自己模拟实现Spring

1.jar文件
dom4j-1.6.1.jar
jaxen-1.1.1.jar===解析xml用的2个jar
commons-logging-1.1.1.jar===日志的jar
spring-beans-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-core-3.2.8.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar===spring最简的4个jar文件

2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">

<bean id="userService" class="com.spring.study.impl.UserServiceImpl" />

</beans>

3.java文件
package com.spring.study;

public interface UserService {

	public void saveUser();
}

package com.spring.study.impl;

import com.spring.study.UserService;

public class UserServiceImpl implements UserService {

	@Override
	public void saveUser() {
		System.out.println("saveUser()方法执行!");
	}

}

public class BeanDefinition {

	private String id;
	private String className;

	public BeanDefinition() {
	}

	public BeanDefinition(String id, String className) {
		this.id = id;
		this.className = className;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}
}

public class CnpcClassPathXmlApplicationContext {

	private List<BeanDefinition> beanDefines = new ArrayList<>();
	private Map<String,Object> singletons = new HashMap<>();
	
	public CnpcClassPathXmlApplicationContext(String fileName){
		<strong>this.readXML(fileName);
		this.instanceBeans();</strong>
	}

	/**
	 * 完成bean的实例化
	 */
	private void instanceBeans() {
		for(BeanDefinition beanDefine: beanDefines){
			if(beanDefine != null && !"".equals(beanDefine.getClassName()))
				try {
					<strong>singletons.put(beanDefine.getId(),Class.forName(beanDefine.getClassName()).newInstance());</strong>
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	/**
	 * 读取xml配置文件
	 * @param fileName
	 */
	@SuppressWarnings("unchecked")
	private void readXML(String fileName) {
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try{
			URL xmlpath = this.getClass().getClassLoader().getResource(fileName);
			document = saxReader.read(xmlpath);
			Map<String,String> nsMap = new HashMap<>();
			nsMap.put("ns", "http://www.springframework.org/schema/beans");//加入命名空间
			XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
			xsub.setNamespaceURIs(nsMap);//设置命名空间
			List<Element> beans = xsub.selectNodes(document);//读取文档下所有bean节点
			for(Element element:beans){
				String id = element.attributeValue("id");//获取id属性值
				String clazz = element.attributeValue("class");//获取class属性值
				BeanDefinition beanDefintion = new BeanDefinition(id,clazz);
				beanDefines.add(beanDefintion);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public Object getBean(String beanName){
		return singletons.get(beanName);
	}
}


@Test
	public void testMySpring() {
		CnpcClassPathXmlApplicationContext context = new <strong>CnpcClassPathXmlApplicationContext</strong>(
				"bean.xml");
		UserService userService = (UserService) context.getBean("userService");
		userService.saveUser();
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值