手撸Spring06: 通过三种资源加载器读取配置文件

上回书说到, 我们每次要注册bean都是代码现场注册去插入,很不方便, 所以我们要开始搞从配置文件里面读出来自动注册,让用户一句话搞定.

BeanDefinitionReader

首先定义一个接口,因为可以有多种方式去读取配置文件

/**
 * 读取 bean定义的接口
 */
public interface BeanDefinitionReader {

    BeanDefinitionRegistry getRegistry();

    ResourceLoader getResourceLoader();

    void loadBeanDefinitions(Resource resource);

    void loadBeanDefinitions(Resource... resources);

    void loadBeanDefinitions(String location);
}

AbstractBeanDefinitionReader

一个抽象的实现类

/**
 * 抽象类实现加载和注册
 */
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader{

    private final BeanDefinitionRegistry registry;

    private final ResourceLoader resourceLoader;

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

再搞一个类去实现抽象方法

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) {
        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) {
        for (Resource resource : resources) {
            loadBeanDefinitions(resource);
        }
    }

    @Override
    public void loadBeanDefinitions(String location) {
        ResourceLoader resourceLoader = getResourceLoader();
        Resource resource = resourceLoader.getResource(location);
        loadBeanDefinitions(resource);
    }

    /**
     * 从流中读取配置
     * @param inputStream
     */
    private 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);
            //读取属性并填充
            NodeList beanNodeList = bean.getChildNodes();
            for (int j = 0; j <beanNodeList.getLength(); j++) {
                if (!(beanNodeList.item(j) instanceof Element)) continue;
                //判断标签是否为属性
                if (!"property".equals(beanNodeList.item(j).getNodeName())) continue;

                Element property = (Element) beanNodeList.item(j);
                String attrName = property.getAttribute("name");
                String attrValue = property.getAttribute("value");
                String attrRef = property.getAttribute("ref");
                //获取属性值
                Object value=StrUtil.isNotEmpty(attrRef)?(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);

        }
    }
}

Resource

还有一个比较关键的是读取配置文件,也是先搞个抽象类

public interface Resource {
    InputStream getInputStream() throws IOException;
}

ClassPathResource

默认的从配置文件里读

public class ClassPathResource implements Resource {

    private final String path;

    private final ClassLoader classLoader;

    public ClassPathResource(String path) {
        this(path, null);
    }

    public ClassPathResource(String path, ClassLoader classLoader) {
        this.path = path;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

    @Override
    //这一部分的实现是用于通过 ClassLoader 读取ClassPath 下的文件信息
    public InputStream getInputStream() throws IOException {
        InputStream is = classLoader.getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException(this.path + " cannot be opened because it does not exist");
        }
        return is;
    }
}

FileSystemResource

根据文件目录读

public class FileSystemResource implements Resource{
    private final File file;

    private final String path;

    public FileSystemResource(File file) {
        this.file = file;
        this.path=file.getPath();
    }

    public FileSystemResource(String path) {
        this.path = path;
        this.file=new File(path);
    }

    @Override
    //通过指定文件路径的方式读取文件信息
    public InputStream getInputStream() throws IOException {
        return new FileInputStream(file);
    }
}

UrlResource

public class UrlResource implements Resource {

    private final URL url;

    public UrlResource(URL url) {
        Assert.notNull(url, "URL must not be null");
        this.url = url;
    }

    //通过 HTTP 的方式读取云服务的文件,我们也可以把配置文件放到 GitHub 或者 Gitee 上。
    @Override
    public InputStream getInputStream() throws IOException {
        URLConnection urlConnection = this.url.openConnection();
        return urlConnection.getInputStream();
    }
}

ResourceLoader

包装一下资源加载器, 根据传进来的地址判断使用哪种工具, 简化外部的使用

/**
 * 资源加载器
 */
public interface ResourceLoader {

    String CLASSPATH_URL_PREFIX ="classpath:";

    Resource getResource(String location);
}

DefaultResourceLoader

一般都会给一个默认的实现方式,

public class DefaultResourceLoader implements ResourceLoader{


    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");
        //如果地址是classPath开头
        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);
            }
        }
    }
}

测试

这样我们就可以读取配置去使用了

@Test
public void testClasspath() throws IOException {
    Resource resource = resourceLoader.getResource("classpath:important.properties");
    InputStream inputStream = resource.getInputStream();
    String s = IoUtil.readUtf8(inputStream);
    System.out.println(s);
}

@Test
public 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);
}

@Test
public 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);
}

@Test
public void testXml(){
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
    reader.loadBeanDefinitions("classpath:spring.xml");
    UserService userService = (UserService) beanFactory.getBean("userService", UserService.class);
    String s = userService.queryUserInfo();
    System.out.println("测试结果"+s);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值