`

Spring中关于classpath:和classpath*:前缀的一个小问题

 
阅读更多

在写Java代码时,有很多场景需要用到从classpath中加载资源。

使用Spring时,定义了比较方便的前缀风格告诉程序从哪里加载。

比较常用的有classpath:和classpath*:两种前缀,用于指定从classpath中加载资源。但两者有些细微差别。

 

以加载Spring的配置文件为例,以下的写法大家应该比较熟悉:

 

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
                "classpath:META-INF/spring/a.bean.xml",
                 "classpath*:META-INF/spring/b.bean.xml" });
 

两种写法是希望在当前程序的classpath下去加载META-INF/spring/a.bean.xml和META-INF/spring/b.bean.xml。

但请注意,前者没有*作为前缀,在classpath下面找不到META-INF/spring/a.bean.xml时,程序会马上抛出FileNotFoundException,提示你文件找不到,这种Fail Fast的风格,在定位问题的时候可能很有帮助。

后者因为加上了*通配符,所以即使在classpath下面找不到META-INF/spring/b.bean.xml时,程序只会偷偷忍掉,不做任何错误提示。当然,这在某些特定的场景下,可能也是有意义的。

 

所以根据不同的使用场景,这两点区别可能需要注意一下。

另外,当某些时候,你怀疑Spring的某些配置文件加载的不对的时候,可以通过spring类库中提供的PathMatchingResourcePatternResolver类来来帮你定位一下,程序到底从哪里加载进来的你指定的配置文件,实例代码如下:

 

        try {
            for (Resource r : new PathMatchingResourcePatternResolver().getResources("your_locationPattern")){
                System.out.println("The real path is : "+r.getFile().getAbsolutePath());
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
分享到:
评论

相关推荐

    毕设项目:基于SpringBoot+MyBatis-Plus 前后端分离的影院选座购票系统.zip

    spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/movie?characterEncoding=utf8&serverTimezone=UTC username: root password: 123456 cache: redis: #...

    maven相关资料

    一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用: ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    classpath*: 前缀 4.7.2.3. 其他关于通配符的说明 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 ...

    Java学习资料-SpringBoot自带模板引擎Thymeleaf使用详解

    - `spring.thymeleaf.prefix`:模板文件的路径前缀,默认为 `classpath:/templates/`。 - `spring.thymeleaf.suffix`:模板文件的后缀,默认为 `.html`。 - `spring.thymeleaf.cache`:是否启用缓存,默认为 `true`...

    springmybatis

    请注意,这里面有一个方法名 selectUserByID 必须与 User.xml 里面配置的 select 的id 对应() 重写测试代码 程序代码 程序代码 public static void main(String[] args) { SqlSession session = ...

    embedded-cassandra-spring-boot-starter:嵌入式Cassandra Spring Boot Starter

    嵌入式Cassandra [Spring Boot Starter] 该项目包括AutoConfiguration 。 要配置CassandraBuilder它建立之前Cassandra ,应用程序性能都可以使用。 所有属性都以前缀cassandra.embedded开头。 # Cassandra config...

    SpringShiro分布式缓存版

    这是一个shiro的入门Demo.. 使用了Spring MVC,mybaits等技术.. 数据库设计 : User : name--password Role : id--userid--roleName Function : id--userid--url tinys普通用户只能访问index.jsp admin用户通过添加...

    seckill-ssm:使用spring+springMVC+MyBatis创建秒杀系统

    1、maven中的classpath是指java和resources下的路径 2、spring声明式事物只回滚运行期异常 3、使用注解控制事物方法的优点: (1) 开发团队达成一致约定,明确标注事务方法的编程风格 (2) 保证事务方法的执行时间尽...

    SpringBoot:springboot基础服务

    2.3将属性文件装配成一个bean,@ ConfigurationProperties(前缀=“ springboot.properties”)3.在spring boot中转换会加载(2的延申)(资源)类路径:/,file:./,file:。 / config /路径下以应用程序命名的...

    好用的代码生成源码

    rapid-framework是一个以spring为核心的项目脚手架(或者称为胶水框架),框架将各个零散的框架(struts,strust2,springmvc,hibernate,ibatis,spring_jdbc,flex)搭建好,并内置一个代码生成器,辅助项目开发,可以生成...

Global site tag (gtag.js) - Google Analytics