spring简单环境搭建 和spring容器解析模拟

spring环境的搭建
需要导入的jar文件为
spring-framework-2.5.6/dist/spring.jar
spring-framework-2.5.6/lib/jakarta-commons/commons-logging.jar

创建spring的XML文件,我取名为bean.xml
头文件可以从例子或文档中拷贝,我从/spring-framework-2.5.6/docs/reference/html_single/index.html文档拷贝

<?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-2.5.xsd">
          
</beans>

创建bean并将其注入到spring xml文件 当中
<bean id="personservice" class="joeho.blog.service.impl.PersonServiceBean"></bean>
bean 中id或name属性都代表bean的别名,其中name属性可以带特殊字符,id中则不能


测试环境是否成功,编写junit实例进行测试
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @Test
 public void instanceSpring(){
  ApplicationContext act = new ClassPathXmlApplicationContext("Beans.xml");
  PersonService ps=(PersonService)act.getBean("personservice");
  ps.save();
  
 }
}


模拟spring容器管理Bean的一个例子

package junit.test;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import joeho.vo.BeanDefinition;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;


public class JoehoaClassPathXMLApplicationContext {
 private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
 private Map<String,Object> singeton = new HashMap<String,Object>();
 
 
 public JoehoaClassPathXMLApplicationContext(String xmlName) {
  this.readXml(xmlName);
  this.instanceBeans();
 }

 /**
  * 读XML文件
  * @param xmlName
  */
 private void readXml(String xmlName) {
  SAXReader saxReader = new SAXReader();
  Document document = null;
  
  try {
   URL xmlPath = this.getClass().getClassLoader().getResource(xmlName);
   document = saxReader.read(xmlPath);
   Map<String,String> nsMap = new HashMap<String,String>();
   nsMap.put("ns", "http://www.springframework.org/schema/beans");//加入命名空间
   XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean的查询路径
   xsub.setNamespaceURIs(nsMap);//设置命名空间
   List<Element> list = xsub.selectNodes(document);//获取文档下的所有BEAN节点
   
   for(Element element:list){
    String id = element.attributeValue("id");//获取ID属性值
    String classzz = element.attributeValue("class");//获取class属性值
    BeanDefinition beanDefin = new BeanDefinition(id,classzz);
    beanDefines.add(beanDefin);
   }
      
  } catch (DocumentException e) {
   e.printStackTrace();
  } 
  
 }
 /**
  * 完成bean的实例化
  */
 private void instanceBeans() {

  for(BeanDefinition beans:beanDefines){
   try {
    if(!beans.getId().equals("")&&beans!=null&&!beans.getClasszz().equals("")){
     singeton.put(beans.getId(), Class.forName(beans.getClasszz()).newInstance());
    }
   } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 
  }
  
 }
 /**
  * 获取Bean实例
  * @param beanName
  * @return
  */
 public Object getBean(String beanName){
  return singeton.get(beanName);
 }
}

package joeho.vo;

public class BeanDefinition {
 private String id;
 private String classzz;
 public BeanDefinition(String id, String classzz) {
  this.id = id;
  this.classzz = classzz;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getClasszz() {
  return classzz;
 }
 public void setClasszz(String classzz) {
  this.classzz = classzz;
 }
 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值