模拟Spring读取Properties文件

转自:http://www.cnblogs.com/shamgod/p/4586971.html

一、目标:读取配置文件properties文件,获得类名来生成对象

二、类

1.Movable.java

1
2
3
public interface Movable {
    void run();
}

  

2.Car.java

1
2
3
4
5
6
7
public class Car implements Movable {
    public void run() {
        System.out.println("Car running...............");
    }
     
}

  

3.spring.properties

PS:"="两边不能有空格

1
vechileType=com.tong.spring.factory.Car

  

4.Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test {
     
    @org.junit.Test
    public void test() {
         
        //读取properties文件,获得类名来生成对象
        Properties pros = new Properties();
         
        try {
            //1.读取要实例化的类名
            pros.load(Test.class.getClassLoader().getResourceAsStream("com/tong/spring/factory/spring.properties"));
            String vechileType = pros.getProperty("vechileType");
             
            //2.利用反射装载类及用newInstance()实例化类
            Object o = Class.forName(vechileType).newInstance();
            Movable m = (Movable)o;
            m.run();
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  

5.运行结果:

6.若改成spring读取xml文件,则spring配置好后,增加applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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-3.0.xsd">
  <bean id="vehicle" class="com.tong.spring.factory.Car">
  </bean>
  <!-- more bean definitions go here -->
</beans>

  

7.Test.java改为如下

1
2
3
4
5
6
7
8
9
10
11
public class Test {
     
    @org.junit.Test
    public void test() {
         
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object o = factory.getBean("vehicle");
        Movable m = (Movable)o;
        m.run();
    }
}

用Jdom模拟Spring

转自:http://www.cnblogs.com/shamgod/p/4587387.html

一、概述

1.目标:模拟Spring的Ioc

2.用到的知识点:利用jdom的xpath读取xml文件,反射

二、有如下文件:

1.applicationContext.xml

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<beans>
  <bean id="vehicle" class="com.tong.spring.factory.Car">
  </bean>
</beans>

  

2.BeanFactory.java

1
2
3
public interface BeanFactory {
    Object getBean(String id);
}

  

3.ClassPathXmlApplicationContext.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class ClassPathXmlApplicationContext implements BeanFactory{
     
    private Map<String, Object> container = new HashMap<String, Object>();
    public ClassPathXmlApplicationContext(String fileName) {
         
        //读取xml文件
        SAXBuilder sb = new SAXBuilder();
        Document doc;
        try {
            //doc = sb.build(fileName);
            //Test.java要改为BeanFactory factory = new ClassPathXmlApplicationContext("com/tong/spring/factory/applicationContext.xml");
            //否则付出现“java.net.MalformedURLException”
            doc = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName));
            Element root = doc.getRootElement();
            List list = XPath.selectNodes(root, "/beans/bean");
            for (int i = 0; i < list.size(); i++) {
                Element element = (Element) list.get(i);
                String id = element.getAttributeValue("id");
                String clazz = element.getAttributeValue("class");
                // 利用反射生成例,然后装进容器
                Object o = Class.forName(clazz).newInstance();
                container.put(id, o);
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public Object getBean(String id) {
        return container.get(id);
    }
}

  

4.Movable.java

1
2
3
public interface Movable {
    void run();
}

  

5.Car.java

1
2
3
4
5
6
7
public class Car implements Movable {
    public void run() {
        System.out.println("Car running...............");
    }
     
}

 

6.Test.java 

1
2
3
4
5
6
7
8
9
10
11
12
public class Test {
     
    @org.junit.Test
    public void test() {
         
        //BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        BeanFactory factory = new ClassPathXmlApplicationContext("com/tong/spring/factory/applicationContext.xml");
        Object o = factory.getBean("vehicle");
        Movable m = (Movable)o;
        m.run();
    }
}

  

  

运行结果:

JDOM 2操作XML:http://www.studytrails.com/java/xml/jdom2/java-xml-jdom2-xpath/

package com.studytrails.xml.jdom;import java.io.IOException;
import java.util.List;import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;public class XPathExample2 {private static String xmlSource = "http://feeds.bbci.co.uk/news/technology/rss.xml?edition=int";public static void main(String[] args) throws JDOMException, IOException {// read the XML into a JDOM2 document.SAXBuilder jdomBuilder = new SAXBuilder();Document jdomDocument = jdomBuilder.build(xmlSource);// use the default implementationXPathFactory xFactory = XPathFactory.instance();// System.out.println(xFactory.getClass());// select all linksXPathExpression<Element> expr = xFactory.compile("//link", Filters.element());List<Element> links = expr.evaluate(jdomDocument);for (Element linkElement : links) {System.out.println(linkElement.getValue());}// select all links in image elementexpr = xFactory.compile("//image/link", Filters.element());List<Element> links2 = expr.evaluate(jdomDocument);for (Element linkElement : links2) {System.out.println(linkElement.getValue());}// get the media namespaceNamespace media = jdomDocument.getRootElement().getNamespace("media");// find all thumbnail elements from the media namespace where the// attribute widht has a value > 60expr = xFactory.compile("//media:thumbnail[@width>60.00]", Filters.element(), null, media);// find the first element in the document and get its attribute named 'url'System.out.println(expr.evaluateFirst(jdomDocument).getAttributeValue("url"));// find the child element of channel whose name is title. find the// descendant of item with name title.Element firstTitle = xFactory.compile("//channel/child::item/descendant::title", Filters.element()).evaluateFirst(jdomDocument);System.out.println(firstTitle.getValue());}}

Java设计模式——工厂模式——模拟Spring相关推荐

  1. java设计模式工厂模式_Java中的工厂设计模式

    java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...

  2. Java设计模式-工厂模式(3)抽象工厂模式

    在Java设计模式-工厂模式(2)工厂方法模式 我们知道了工厂方法模式解决了简单工厂模式中的缺陷,做到了满足开闭原则,但是时代是进步的,进而又产生新的问题,工厂难道只能生产一种东西吗.我们所见到的工厂 ...

  3. Java设计模式-工厂模式(2)工厂方法模式

    在Java设计模式-工厂模式(1)简单工厂模式 中我们介绍了简单工厂模式,提到了简单工厂模式违背了开闭原则,而"工厂方法模式"是对简单工厂模式的进一步抽象化,其好处是可以使系统在不 ...

  4. Java设计模式-工厂模式(1)简单工厂模式

    Java设计模式-工厂模式(1)简单工厂模式 一.前言 1)例子 2)类图关系 3)代码实现 二.简单工厂模式 2.1.概述: 2.2.类图关系: 2.3.代码修改: 2.4.优缺点 2.5.扩展-简 ...

  5. java设计模式工厂模式_Java中的复合设计模式

    java设计模式工厂模式 Composite pattern is one of the Structural design pattern. Composite design pattern is ...

  6. java设计模式工厂模式_Java中的桥梁设计模式

    java设计模式工厂模式 Today we will look into Bridge Design Pattern in java. When we have interface hierarchi ...

  7. java设计模式工厂模式_Java中的外观设计模式

    java设计模式工厂模式 Facade Design Pattern is one of the Structural design patterns (such as Adapter pattern ...

  8. 10.Java设计模式 工厂模式,单例模式

    Java 之工厂方法和抽象工厂模式 1. 概念 工厂方法:一抽象产品类派生出多个具体产品类:一抽象工厂类派生出多个具体工厂类:每个具体工厂类只能创建一个具体产品类的实例. 即定义一个创建对象的接口(即 ...

  9. java 工厂模式的写法_[java设计模式] 工厂模式解析

    什么是工厂模式? 我的总结是: 遵守软件设计中的开闭原则和依赖反转原则, 并且客户端只需通过参数来创造多个对象, 并且在创建过程中,创建对象的过程对客户端是透明的. 这种开发模式叫做工厂模式. 出现原 ...

  10. 学习:java设计模式—工厂模式

    一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Facto ...

最新文章

  1. 又有六所大学考研预调剂系统已开放!
  2. nlp 优缺点 混淆度_NLP中文分词的评估指标
  3. LeetCode求能够装得最多的水
  4. git add * 提示warning: LF will be replaced by CRLF in 解决办法
  5. 小小算法题(CCF)
  6. 在SpringMVC中使用@RequestBody和@ResponseBody注解处理json时,报出HTTP Status 415的解决方案
  7. 第九次作业(杨辉三角)
  8. C++_实现一个简单的智能指针shared_ptr
  9. java网上购物系统_Java Web 应用教程——网上购物系统的实现
  10. git 对比两个分支差异
  11. 在linux下安装TPLINK无线网卡驱动
  12. ps快速放大缩小图片
  13. 苹果怎么用计算机,苹果笔记本怎么用(苹果电脑Mac 系统基本操作介绍)
  14. 在GitHub上建立自己在线简历
  15. 计算机控制器及其设计实现
  16. 望而生畏的C语言在逐渐凋零
  17. VB学习第四周续--四位整数逆序
  18. 解决git commit提示Please tell me who you are
  19. k8s部署-31-k8s中如何进行资源隔离资源
  20. Excel导出报错 You can define up to 4000 styles in a .xls workbook

热门文章

  1. (5)exec函数详解
  2. 导出数据提示--secure-file-priv选项问题的解决方法
  3. 个人博客网站搭建-WordPress-NameSilo-云左虚拟主机
  4. CSDN独家全网首发专栏 | 《目标检测YOLO改进指南》改进涨点推荐!
  5. Lync添加自定义菜单
  6. 多层感知机的从零开始实现( 从D2L 包中抽取函数)
  7. pip指定网址下载安装(清华源)
  8. IT 面试常见IQ试题
  9. ps修改图片上文字的几种方法 图+文
  10. GitHub · 如何创建文件夹