代码参考自

黄亿华大神的<<1000行代码读懂Spring(一)- 实现一个基本的IoC容器>>

原网页如下

http://my.oschina.net/flashsword/blog/192551

package com.myspring;   

import java.io.FileInputStream;
import java.io.InputStream;   

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;   

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;   

public class XmlReader {
    public static void main(String[] args) {
        new XmlReader();
    }
    public XmlReader(){
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder domBuilder = domfac.newDocumentBuilder();   

            //默认是工程目录
            InputStream is =new FileInputStream("bin/resources/tinyioc.xml");
            Document doc = domBuilder.parse(is);
            Element root = doc.getDocumentElement();
            NodeList beans = root.getChildNodes();
            if(beans!=null)
                for (int i = 0; i < beans.getLength(); i++) {
                	Node bean =  beans.item(i);
                    if (bean.getNodeName().equals("bean")) {
                    	Element el=(Element) bean;
                    	System.out.println( el.getAttribute("id") +"  "+el.getAttribute("class") );
                    	NodeList propertyNode = el.getElementsByTagName("property");
                    	for (int j = 0; j < propertyNode.getLength(); j++)
                    		if(propertyNode.item(j) instanceof Element){
                    			Element e=(Element) propertyNode.item(j);
                    			String name=e.getAttribute("name");
                    			String value=e.getAttribute("value").equals("")?e.getAttribute("ref"):e.getAttribute("value");
                    			System.out.println("       "+name+"  "+value );
                    		}	

					}

                }   

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    }
}  

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" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="helloWorldService" class="com.myspring.HelloWorldServiceImpl">
        <property name="text" value="Hello World!"></property>
        <property name="out" ref="outputService"></property>
    </bean>

    <bean id="outputService" class="com.myspring.OutputService">
    </bean>

    <bean id="autoProxyCreator" class="us.codecraft.tinyioc.aop.AspectJAwareAdvisorAutoProxyCreator"></bean>

    <bean id="timeInterceptor" class="us.codecraft.tinyioc.aop.TimerInterceptor"></bean>

    <bean id="aspectjAspect" class="us.codecraft.tinyioc.aop.AspectJExpressionPointcutAdvisor">
        <property name="advice" ref="timeInterceptor"></property>
        <property name="expression" value="execution(* us.codecraft.tinyioc.*.*(..))"></property>
    </bean>

</beans>

运行结果如下

helloWorldService  com.myspring.HelloWorldServiceImpl

       text  Hello World!

       out  outputService

outputService  com.myspring.OutputService

autoProxyCreator  us.codecraft.tinyioc.aop.AspectJAwareAdvisorAutoProxyCreator

timeInterceptor  us.codecraft.tinyioc.aop.TimerInterceptor

aspectjAspect  us.codecraft.tinyioc.aop.AspectJExpressionPointcutAdvisor

       advice  timeInterceptor

       expression  execution(* us.codecraft.tinyioc.*.*(..))

最新文章

  1. 数据库备份并分离日志表(按月)sh 脚本
  2. Win10桌面预览版14316更新内容大全
  3. Java程序的安装、配置、创建项目
  4. SNMP-配置文件详解
  5. pvresize - Unix, Linux Command
  6. 不容易系列之(3)—— LELE的RPG难题
  7. jquery.validate新的写法(jquery.validate1.13.js)
  8. 第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承
  9. 初学者的checklist:对于QTP,你应该知道的9个基本概念
  10. 条件与(&amp;&amp;)和逻辑与(&amp;)以及条件或(||)和逻辑或(|)区别
  11. iOS 百度地图监听地图状态
  12. C# 文件/文件夹压缩
  13. oracle 高级分组
  14. 禁用Java DNS缓存-Disable DNS caching
  15. HDU 3584 三维树状数组
  16. oracle 查询哪些表分区
  17. 不用分支语句实现1+2+。。。+n
  18. java 取汉字首字母
  19. 浅谈模块系统与 ABP 框架初始化
  20. Markdown 使用技巧

热门文章

  1. Xcode 调试技巧 --常用命令和断点
  2. java创建线程
  3. [Pelican]Pelican入门(二)
  4. zk日常运维管理
  5. Android Multimedia框架总结(七)C++中MediaPlayer的C/S架构补充及MediaService介绍
  6. Ajax框架,DWR介绍,应用,例子
  7. Building System之 get_abs_build_var() &amp;&amp; get_build_var()
  8. x264源代码简单分析:编码器主干部分-1
  9. 20 ViewPager总结
  10. 在非ViewController中显示AlertController的方法