引入dom4j jar包

1、新建Person接口和PersonBean

public interface PersonIService {
public void helloSpring();
}
public class PersonServiceImpl implements PersonIService {

	@Override
public void helloSpring() {
System.out.println("Hello Spring!");
}
}

2、在beans.xml中注入PersonBean

<bean id="personIService" class="cn.server.impl.PersonServiceImpl" />

3、新建BeanDefinition  用于存放从beans.xml中获取的beans

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;
} }

5、新建 OhterClassPathXMLApplicationContext 用于解析beans.xml

public class OhterClassPathXMLApplicationContext {
private List<BeanDefinition> list=new ArrayList<BeanDefinition>();
private Map<String,Object> beans=new HashMap<String, Object>(); public OhterClassPathXMLApplicationContext(String fileName){
this.readXML(fileName);
this.instanceBeans();
}
private void instanceBeans(){
for(BeanDefinition bean : list){
try {
// 创建Bean实例,并放到Map中
if(bean.getClassName()!=null && !bean.getClassName().trim().equals("")){
beans.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void readXML(String fileName){
SAXReader saxReader = new SAXReader();
Document doc=null;
try{
// JAVA里面对于类进行调用配置资源的文件数据,以this.getClass().getResourceAsStream()来读取比较合适。
// 路径采用相对路径直接可以从工程的path路径去找。
URL xmlpath=this.getClass().getClassLoader().getResource(fileName);
doc=saxReader.read(xmlpath);
Map<String,String> nsMap=new HashMap<String,String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans"); // 加入命名空间
XPath xsub=doc.createXPath("//ns:beans/ns:bean"); // 创建 beans/bean的查询路径
xsub.setNamespaceURIs(nsMap); // 设置命名空间
List beans=xsub.selectNodes(doc); // 获取文档下的所有bean节点
for(Object node : beans){
Element element=(Element)node;
String id=element.attributeValue("id");
String className=element.attributeValue("class");
BeanDefinition bean=new BeanDefinition(id,className);
list.add(bean);
} }catch(Exception e){
e.printStackTrace();
}
}
// 通过Id名称,获取Bean
public Object getBean(String name){
return beans.get(name);
}
}

6、测试代码

	@Test
public void test2() {
OhterClassPathXMLApplicationContext otherContext=new OhterClassPathXMLApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)otherContext.getBean("personIService");
personIService.helloSpring();
}

Spring、编码剖析Spring管理Bean的原理的更多相关文章

  1. (转)编码剖析Spring管理Bean的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...

  2. (转)编码剖析Spring依赖注入的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...

  3. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

  4. (转)编码剖析Spring装配基本属性的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52856465 上回我们已经讲到了Spring依赖注入的第一种方式,现在我们来详解第二种方式,须 ...

  5. (转)编码剖析@Resource注解的实现原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52860046 上文我们已经学会使用@Resource注解注入属性.学是学会了,但也仅限于会使用 ...

  6. Spring(八)编码剖析@Resource注解的实现原理

    配置文件beans2.xml <?xml version="1.0" encoding="UTF-8"? > <beans xmlns=&qu ...

  7. 【Sping管理bean的原理】

    spring容器默认情况下,当服务启动时,解析配置文件,实例化文件中的所有类. 我们直接使用spring时,获取spring注入的bean是这样的, ApplicationContext ctx =  ...

  8. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  9. 编码剖析Spring管理bean的原理

    project目录 MyClassPathXMLApplicationContext读取xml,以及实例化bean. 因为是一开始实例化配置文件所有bean,所以需要构造器完成这些工作. packag ...

随机推荐

  1. QTDesigner的QVBoxLayout自动随窗口拉伸

    在MainWindow的构造函数中添加如下代码://设置Uiui.setupUi(this); //使Ui可自适应父窗口大小QVBoxLayout* mainLayout = new QVBoxLay ...

  2. MFC 动态创建按钮

    首先在对话框(模式对话框,无模式对话框)中添加一个ADD按钮,通过点击按钮产生的通告消息调用::OnBtnAdd()方法.此方法会在对话框的左上角创建一个按钮. 当然首先要在和次对话框相关联的类中添加 ...

  3. gem update --system 302 错误 解决方案(转)

    具体过程如下: 1.InstantRails-2.0安装后,在配置环境变量path中配置ruby/bin目录(如果系统中有多个RUBY,执行命令行的时候系统认的就是path中的) 2.进入DOS命令行 ...

  4. Red5 1.0 RC1 与tomcat 6 整合

    1.0以上版本没整合成功过,如有人整合成功过,也分享下,在此先谢谢 一.下载red5-1.0.0-RC1.zip 下载地址:http://code.google.com/p/red5/ 和http:/ ...

  5. Hadoop 3、Hadoop 分布式存储系统 HDFS

    HDFS是Hadoop Distribute File System 的简称,也就是Hadoop的一个分布式文件系统. 一.HDFS的优缺点 1.HDFS优点: a.高容错性 .数据保存多个副本 .数 ...

  6. poj2488 A Knight's Journey

      A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24840   Accepted:  ...

  7. zoj 3229 有源汇有上下界的最大流模板题

    /*坑啊,pe的程序在zoj上原来是wa. 题目大意:一个屌丝给m个女神拍照.计划拍照n天,每一天屌丝最多个C个女神拍照,每天拍照数不能超过D张,并且给每一个女神i拍照有数量限制[Li,Ri], 对于 ...

  8. ExtJs3学习资料分享

    最近在学习EXTJS3,在网上找了一些pdf的书.不过网上分享的有些书都是Ext2.0的.Ext3的比较少.有些书也不全.很多是样章.最近找到一本分享的书<ExtJS源码分析与开发实例宝典> ...

  9. Android 基本控件

    http://www.cnblogs.com/LT-blogs/archive/2012/08/07/2626118.html http://blog.csdn.net/android_tutor/a ...

  10. sctf pwn400

    这个题目在这个链接中分析得很透彻,不再多余地写了.http://bruce30262.logdown.com/posts/245613-sctf-2014-pwn400 exploit: from s ...