BeanDifinition中的resource定位

ClassPathResource res = new ClassPathResource("bean.xml");
DefaultListAbleFacttory dlaf = new DefaultListAbleFacttory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(dlaf);
reader.loadBeanDefinitions(res);

从上面编程的方式来说,如果要找到BeanDefiniton的定位,需要使用DefaultListAbleFactory,同时也是需要使用Resource接口的
在上面的是使用ClassPathResourcee这个类的,其实对资源的定位并不是有DefaultListAbleFacotory直接使用的,而是通过XmlBeanDefinitionReader来处理的
换一句话说就是DefaultListAbleFactory只是一个单纯的容器,需要为他匹配相应的Reader才能够使用它

从我们常用ApplicationContext 中的FileSystemXmlApplicationContext可以文件系统,ClassPathXmlApplicationContext 从classPath读取Resource , XmlWebApplicationContext
可以从web容器中载入resource

下面主要是从FileSystemXmlApplicationContext来讲述资源定位
FileSystemXmlApplicationContext已经通过继承AbstractApplicationContext具备了ResourceLoader读入以Resource定义的BeanDifinition的能力(具体的是指代DefaultResourceLoader这个接口的功能)
以下就是FileSystemXmlApplicaition的源代码BeanFactory系列的容器

public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {
    public FileSystemXmlApplicationContext() {
    }

    public FileSystemXmlApplicationContext(ApplicationContext parent) {
        super(parent);
    }

    //configLocation包含的是BeanDifinition的路径
    public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
        this(new String[]{configLocation}, true, (ApplicationContext)null);
    }

   //包含多个BeanDifinition的路径
    public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {
        this(configLocations, true, (ApplicationContext)null);
    }

    
    public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
        this(configLocations, true, parent);
    }

    public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
        this(configLocations, refresh, (ApplicationContext)null);
    }

     //调用了refresh启动容器,并且载入beanDefinition
    public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
        super(parent);
        this.setConfigLocations(configLocations);
        if (refresh) {
            this.refresh();
        }

    }

    //通过文件路径获取resource
    protected Resource getResourceByPath(String path) {
        if (path != null && path.startsWith("/")) {
            path = path.substring(1);
        }

        return new FileSystemResource(path);
    }
}

具体实现的编程,就是通过Refresh()中调用了refreshBean()方法,在这个方法中同时又实用createBeanFactory创建一个容器,供给ApplicationContext使用,这个容器也就是我们常说的DefaultListAbleFacotry容器,同时它也可以使用loadBeanDefinitio来载入BeanDifinition,下面的refresh方法其实在new 对象时候已经启动了
在这里插入图片描述
其实涉及到资源的定位主要就是在AbstractRefreshBeanFacotoryContext中的getResourceLoader()方法吗,这个方法其实使用的就是DefaultResource方法

//DefaultResource,我去源码中去没有查询到该行源码
ResourceLoader resourceLoader = getResouirce() ;
//在上面这行代码上调用的
Url url = new Url(location);
return new UrlResource(url);
//说明就是利用io来获取resourcei资源定位

另外需要说明的就是getResourceByPath()会被子类FileSystemXmlApplicationContext实现的,返回的是FileSystemResource对象,通过这个对象可以实现相关的io操作,万完成BeanDifinition的定位

//注意看下面实现的代码
protected Resource getResourceVypath(String path){
    if(path != null && path.startWith("/")){
        path = path.substring(1);
    }
  return new FileSystemResource(path);
}
主要的io操作有
getUrl(); getFileName(); isOpen();v isReadAble();

总结:
具体的代码思路流程
1:以FileSystem方式存在的Resource的定位实现
2:在BeanDefinition定位完成的基础上,通过返回的Resource来进行BeanDefinition
3:在定位过程完成之后,为BeanDifinition的载入创造i/o操作条件

在 MyBatis ,`resource` 是用于加载外部资源文件的一个属性。在 MyBatis 的配置文件,我们经常需要引用外部的资源文件,例如数据库连接信息、SQL 语句等。使用 `resource` 属性可以指定这些外部资源文件的路径,然后在 MyBatis 的配置文件进行引用。 `resource` 属性可以与 `url` 属性一起使用,用于指定外部资源文件的路径。当 `resource` 和 `url` 属性同时存在时,MyBatis 会优先使用 `resource` 属性指定的资源文件。 以下是一个简单的使用 `resource` 属性加载外部资源文件的示例: ```xml <configuration> <properties resource="jdbc.properties" /> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mappers/UserMapper.xml"/> </mappers> </configuration> ``` 在上面的示例,`resource` 属性被用于加载两个外部资源文件:`jdbc.properties` 和 `UserMapper.xml`。其,`jdbc.properties` 文件用于配置数据库连接信息,`UserMapper.xml` 文件用于定义 SQL 映射。 总之,在 MyBatis ,`resource` 属性用于加载外部资源文件,可以方便地配置数据库连接信息、SQL 映射等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值