Spring 框架(二)

3. 包装资源加载器

按照资源加载的不同方式,资源加载器可以把这些方式集中到统一的类服务下进行处理,外部用户只需要传递资源地址即可,简化使用。

定义接口:cn.bugstack.springframework.core.io.ResourceLoader

public interface ResourceLoader {
    /**     * Pseudo URL prefix for loading from the class path: "classpath:"     */    String CLASSPATH_URL_PREFIX = "classpath:";
    Resource getResource(String location);
}

复制代码

  • 定义获取资源接口,里面传递 location 地址即可。

实现接口:cn.bugstack.springframework.core.io.DefaultResourceLoader

public class DefaultResourceLoader implements ResourceLoader {
    @Override    public Resource getResource(String location) {        Assert.notNull(location, "Location must not be null");        if (location.startsWith(CLASSPATH_URL_PREFIX)) {            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));        }        else {            try {                URL url = new URL(location);                return new UrlResource(url);            } catch (MalformedURLException e) {                return new FileSystemResource(location);            }        }    }
}

复制代码

  • 在获取资源的实现中,主要是把三种不同类型的资源处理方式进行了包装,分为:判断是否为 ClassPath、URL 以及文件。

  • 虽然 DefaultResourceLoader 类实现的过程简单,但这也是设计模式约定的具体结果,像是这里不会让外部调用放知道过多的细节,而是仅关心具体调用结果即可。

4. Bean 定义读取接口

cn.bugstack.springframework.beans.factory.support.BeanDefinitionReader

public interface BeanDefinitionReader {
    BeanDefinitionRegistry getRegistry();
    ResourceLoader getResourceLoader();
    void loadBeanDefinitions(Resource resource) throws BeansException;
    void loadBeanDefinitions(Resource... resources) throws BeansException;
    void loadBeanDefinitions(String location) throws BeansException;
}

复制代码

  • 这是一个 Simple interface for bean definition readers. 其实里面无非定义了几个方法,包括:getRegistry()、getResourceLoader(),以及三个加载 Bean 定义的方法。

  • 这里需要注意 getRegistry()、getResourceLoader(),都是用于提供给后面三个方法的工具,加载和注册,这两个方法的实现会包装到抽象类中,以免污染具体的接口实现方法。

5. Bean 定义抽象类实现

cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader

public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
    private final BeanDefinitionRegistry registry;
    private ResourceLoader resourceLoader;
    protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {        this(registry, new DefaultResourceLoader());    }
    public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {        this.registry = registry;        this.resourceLoader = resourceLoader;    }
    @Override    public BeanDefinitionRegistry getRegistry() {        return registry;    }
    @Override    public ResourceLoader getResourceLoader() {        return resourceLoader;    }
}

复制代码

  • 抽象类把 BeanDefinitionReader 接口的前两个方法全部实现完了,并提供了构造函数,让外部的调用使用方,把 Bean 定义注入类,传递进来。

  • 这样在接口 BeanDefinitionReader 的具体实现类中,就可以把解析后的 XML 文件中的 Bean 信息,注册到 Spring 容器去了。以前我们是通过单元测试使用,调用 BeanDefinitionRegistry 完成 Bean 的注册,现在可以放到 XMl 中操作了

6. 解析 XML 处理 Bean 注册

cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader

public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
    public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {        super(registry);    }
    public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {        super(registry, resourceLoader);    }
    @Override    public void loadBeanDefinitions(Resource resource) throws BeansException {        try {            try (InputStream inputStream = resource.getInputStream()) {                doLoadBeanDefinitions(inputStream);            }        } catch (IOException | ClassNotFoundException e) {            throw new BeansException("IOException parsing XML document from " + resource, e);        }    }
    @Override    public void loadBeanDefinitions(Resource... resources) throws BeansException {        for (Resource resource : resources) {            loadBeanDefinitions(resource);        }    }
    @Override    public void loadBeanDefinitions(String location) throws BeansException {        ResourceLoader resourceLoader = getResourceLoader();        Resource resource = resourceLoader.getResource(location);        loadBeanDefinitions(resource);    }
    protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException {        Document doc = XmlUtil.readXML(inputStream);        Element root = doc.getDocumentElement();        NodeList childNodes = root.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {            // 判断元素            if (!(childNodes.item(i) instanceof Element)) continue;            // 判断对象            if (!"bean".equals(childNodes.item(i).getNodeName())) continue;                        // 解析标签            Element bean = (Element) childNodes.item(i);            String id = bean.getAttribute("id");            String name = bean.getAttribute("name");            String className = bean.getAttribute("class");            // 获取 Class,方便获取类中的名称            Class<?> clazz = Class.forName(className);            // 优先级 id > name            String beanName = StrUtil.isNotEmpty(id) ? id : name;            if (StrUtil.isEmpty(beanName)) {                beanName = StrUtil.lowerFirst(clazz.getSimpleName());            }
            // 定义Bean            BeanDefinition beanDefinition = new BeanDefinition(clazz);            // 读取属性并填充            for (int j = 0; j < bean.getChildNodes().getLength(); j++) {                if (!(bean.getChildNodes().item(j) instanceof Element)) continue;                if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) continue;                // 解析标签:property                Element property = (Element) bean.getChildNodes().item(j);                String attrName = property.getAttribute("name");                String attrValue = property.getAttribute("value");                String attrRef = property.getAttribute("ref");                // 获取属性值:引入对象、值对象                Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue;                // 创建属性信息                PropertyValue propertyValue = new PropertyValue(attrName, value);                beanDefinition.getPropertyValues().addPropertyValue(propertyValue);            }            if (getRegistry().containsBeanDefinition(beanName)) {                throw new BeansException("Duplicate beanName[" + beanName + "] is not allowed");            }            // 注册 BeanDefinition            getRegistry().registerBeanDefinition(beanName, beanDefinition);        }    }
}

复制代码

XmlBeanDefinitionReader 类最核心的内容就是对 XML 文件的解析,把我们本来在代码中的操作放到了通过解析 XML 自动注册的方式。

  • loadBeanDefinitions 方法,处理资源加载,这里新增加了一个内部方法:doLoadBeanDefinitions,它主要负责解析 xml

  • 在 doLoadBeanDefinitions 方法中,主要是对 xml 的读取 XmlUtil.readXML(inputStream) 和元素 Element 解析。在解析的过程中通过循环操作,以此获取 Bean 配置以及配置中的 id、name、class、value、ref 信息。

  • 最终把读取出来的配置信息,创建成 BeanDefinition 以及 PropertyValue,最终把完整的 Bean 定义内容注册到 Bean 容器:getRegistry().registerBeanDefinition(beanName, beanDefinition)

五、测试

1. 事先准备

cn.bugstack.springframework.test.bean.UserDao

public class UserDao {
    private static Map<String, String> hashMap = new HashMap<>();
    static {        hashMap.put("10001", "小傅哥");        hashMap.put("10002", "八杯水");        hashMap.put("10003", "阿毛");    }
    public String queryUserName(String uId) {        return hashMap.get(uId);    }
}

复制代码

cn.bugstack.springframework.test.bean.UserService

public class UserService {
    private String uId;
    private UserDao userDao;
    public void queryUserInfo() {        return userDao.queryUserName(uId);    }
    // ...get/set}

复制代码

  • Dao、Service,是我们平常开发经常使用的场景。在 UserService 中注入 UserDao,这样就能体现出 Bean 属性的依赖了。

2. 配置文件

important.properties

# Config Filesystem.key=OLpj9823dZ

复制代码

spring.xml

<?xml version="1.0" encoding="UTF-8"?><beans>
    <bean id="userDao" class="cn.bugstack.springframework.test.bean.UserDao"/>
    <bean id="userService" class="cn.bugstack.springframework.test.bean.UserService">        <property name="uId" value="10001"/>        <property name="userDao" ref="userDao"/>    </bean>
</beans>

复制代码

  • 这里有两份配置文件,一份用于测试资源加载器,另外 spring.xml 用于测试整体的 Bean 注册功能。

3. 单元测试(资源加载)

案例

private DefaultResourceLoader resourceLoader;      
@Beforepublic void init() {    resourceLoader = new DefaultResourceLoader();}   
@Testpublic void test_classpath() throws IOException {    Resource resource = resourceLoader.getResource("classpath:important.properties");    InputStream inputStream = resource.getInputStream();    String content = IoUtil.readUtf8(inputStream);    System.out.println(content);}   
@Testpublic void test_file() throws IOException {    Resource resource = resourceLoader.getResource("src/test/resources/important.properties");    InputStream inputStream = resource.getInputStream();    String content = IoUtil.readUtf8(inputStream);    System.out.println(content);}    
@Testpublic void test_url() throws IOException {    Resource resource = resourceLoader.getResource("https://github.com/fuzhengwei/small-spring/important.properties"    InputStream inputStream = resource.getInputStream();    String content = IoUtil.readUtf8(inputStream);    System.out.println(content);}

复制代码

测试结果

# Config Filesystem.key=OLpj9823dZ
Process finished with exit code 0

复制代码

  • 这三个方法:test_classpath、test_file、test_url,分别用于测试加载 ClassPath、FileSystem、Url 文件,URL 文件在 Github,可能加载时会慢

4. 单元测试(配置文件注册 Bean)

案例

@Testpublic void test_xml() {    // 1.初始化 BeanFactory    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 2. 读取配置文件&注册Bean    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);    reader.loadBeanDefinitions("classpath:spring.xml");
    // 3. 获取Bean对象调用方法    UserService userService = beanFactory.getBean("userService", UserService.class);    String result = userService.queryUserInfo();    System.out.println("测试结果:" + result);}

复制代码

测试结果

测试结果:小傅哥
Process finished with exit code 0

复制代码

  • 在上面的测试案例中可以看到,我们把以前通过手动注册 Bean 以及配置属性信息的内容,交给了 new XmlBeanDefinitionReader(beanFactory) 类读取 Spring.xml 的方式来处理,并通过了测试验证。

六、总结

  • 此时的工程结构已经越来越有 Spring 框架的味道了,以配置文件为入口解析和注册 Bean 信息,最终再通过 Bean 工厂获取 Bean 以及做相应的调用操作。

  • 关于案例中每一个步骤的实现小傅哥这里都会尽可能参照 Spring 源码的接口定义、抽象类实现、名称规范、代码结构等,做相应的简化处理。这样大家在学习的过程中也可以通过类名或者接口和整个结构体学习 Spring 源码,这样学习起来就容易多了。

  • 看完绝对不等于会,你只有动起手来从一个小小的工程框架结构,敲到现在以及以后不断的变大、变多、变强时,才能真的掌握这里面的知识。另外每一个章节的功能实现都会涉及到很多的代码设计思路,要认真去领悟。当然实践起来是最好的领悟方式!

  • 28
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值