Spring中classpath和classpath*的问题
来源:互联网 发布:mac目录结构 编辑:程序博客网 时间:2024/05/20 00:52
这一段是从网上抄录(原网址为 关于Spring加载classpath与classpath*的过程剖析)
1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
3.文件名字包含通配符资源(如果spring-.xml,spring.xml), 如果根目录为”“, classpath加载不到任何资源,
而classpath*则可以加载到classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源第1,2点比较好表理解,大家可以自行测试,第三点表述有点绕,举个例,现在有资源文件结构如下:
classpath:notice*.txt 加载不到资源
classpath*:notice*.txt 加载到resource根目录下notice.txt
classpath:META-INF/notice*.txt 加载到META-INF下的一个资源(classpath是加载到匹配的第一个资源,就算删除classpath下的notice.txt,他仍然可以加载jar包中的notice.txt)
classpath:META-/notice.txt 加载不到任何资源
classpath*:META-INF/notice*.txt 加载到classpath以及所有jar包中META-INF目录下以notice开头的txt文件
classpath*:META-/notice.txt 只能加载到classpath下 META-INF目录的notice.txt
下面是个人测试
这里只记录引用资源文件resources时的区别,简单写了个连接数据库测试一下
目录结构大体如下:
general.properties文件中只是数据库的信息
#DataSourcejdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/shoes?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=123456
spring-hibernate.xml则是spring获取连接池的配置
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="classpath:general.properties"/> <!-- 配置Druid连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 基本属性 driverClassName、url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置 Hibernate 的 SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置 Hibernate 的参数: 方言, 显示SQL等 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 配置映射文件 --> <property name="mappingLocations"> <list> <value>classpath:hbm/Factory.hbm.xml</value> </list> </property> </bean> <!-- 配置HibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean></beans>
主要就是通过<context:property-placeholder location="classpath:general.properties"/>
获取general.properties中内容,然后放入dataSource的bean中。
实测1:
general.properties位置为/resources/general.properties
实测2:
general.properties位置为/resources/general/general.properties
实测3:
general.properties拆成两份
#DataSourcejdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/shoes?useUnicode=true&characterEncoding=UTF-8
#DataSourcejdbc.username=rootjdbc.password=123456
位置为
/resources/general/general.properties和
/resources/general/general1.properties
实测4:
general.properties如上拆成两份重名文件,位置为
/resources/test/general/general.properties和
/resources/test/general1/general.properties
测试结果:
1、classpath在使用时,第一级路径无法使用通配符,如classpath:general*.properties或者classpath:general*/general*.properties是无法获取到的
2、在我将general.properties拆成两个文件后,classpath同样可以获取两个文件,读取到全部内容,不知道是不是我测的方法有问题,这方面没感觉出来说是只能获取到第一个文件的限制
3、classpath比classpath*快,明确位置比通配符快。
- Spring中classpath和classpath*的问题
- Spring中classpath*和classpath的区别
- spring配置中classpath和classpath*的区别
- spring中classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- Spring中关于classpath:和classpath*:前缀的一个小问题
- Spring的classpath和classpath*的区别
- Spring中classpath*:与classpath:的区别
- spring中classpath与classpath*的区别
- spring classpath和classpath*的区别
- Spring中classpath路径问题
- spring中classpath与classpath*
- Spring下面的classpath 和 classpath* 区别的简单讲解
- spring引用配置文件的时候classpath:和classpath*:的区别
- Spring下面的classpath 和 classpath* 区别的简单讲解
- spring加载资源文件中classpath*与classpath的区别
- 转发-摇摆少年梦的技术博客 【专注于Scala语言、Hadoop及Spark大数据处理技术】
- 如何书写论文中的参考文献并正确标注页码
- bonnie++安装报错zcav.cpp:73: error: ‘strdup’ was not declared in this scope
- PL/SQL Developer 1105-32/64位软件下载及激活码
- cydia闪退解决方案
- Spring中classpath和classpath*的问题
- Linux MySQL安装
- Spring的applicationContext.xml详解
- JavaBean 浅析
- leetcode 515. Find Largest Value in Each Tree Row
- Android Studio更新后打开老项目报错,但是能运行
- HDU6069(数学)
- C语言中的指针和内存泄漏
- iOS UILabel 中的文本设置固定宽度