spring中classpath和classpath*的配置区别

简介: spring中classpath和classpath*的配置区别作者: Michael 日期: 2013 年 2 月 26 日发表评论 (0)查看评论在使用spring时,经常会看到类似 classpth:、classpath*: 这样的前缀,不管是加载spring xml配置文件还是其配置文件中加载资源文件都会看到这两种前缀配置,其实这两种前缀是有区别的,下面将举例详细解释。

spring中classpath和classpath*的配置区别

在使用spring时,经常会看到类似 classpth:、classpath*: 这样的前缀,不管是加载spring xml配置文件还是其配置文件中加载资源文件都会看到这两种前缀配置,其实这两种前缀是有区别的,下面将举例详细解释。

[一]、测试项目准备

我们以spring中加载properties资源文件为例讲解,目录结构大致如下:

src
├─main
│  ├─filters
│  │
│  ├─java
│  │  └─com
│  │      └─micmiu
│  │          ├─demoweb
│  │          │  │ ....
│  │          │  │
│  │          │  └─utils
│  │          │
│  │          └─modules
│  │
│  ├─resources
│  │  │  application.properties
│  │  │  applicationContext-shiro.xml
│  │  │  applicationContext.xml
│  │  │  hibernate.cfg.xml
│  │  │  log4j.properties
│  │  │  spring-mvc.xml
│  │  │  spring-view.xml
│  │
│  └─webapp
│      │
│      └─WEB-INF
│
└─test
    ├─java
    │  └─com
    │      └─micmiu
    │          ├─demoweb
    │          │      TestOther.java
    │
    └─resources
            application.properties

同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:

micmiu-test.jar
│  application.properties
│
├─com
│  └─micmiu
│      └─test
│              application.properties
│              RunApp.class
│
└─META-INF
        MANIFEST.MF

从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件

[二]、测试代码:TestClassPath.java

1 package com.micmiu.demoweb;
2  
3 import org.springframework.context.ApplicationContext;
4 importorg.springframework.context.support.ClassPathXmlApplicationContext;
5  
6 /**
7  *
8  * @author <a href="http://www.micmiu.com">Michael Sun</a>
9  */
10 public class TestClassPath {
11  
12     /**
13      * @param args
14      */
15     public static void main(String[] args) {
16         ApplicationContext ctx = new ClassPathXmlApplicationContext(
17                 "classpath:/applicationContext.xml");
18         System.out.println(ctx.getClassLoader().getResource("").getPath());
19  
20     }
21 }

[三]、测试结果

spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:

第一种:classpath:

<context:property-placeholder ignore-unresolvable="true"
    location="classpath:/application.properties" />

这种配置下运行测试代码,日志信息中有关加载properties资源文件只有一条 如下:

Loading properties file from class path resource [application.properties]

第二种: classpath*:

<context:property-placeholder ignore-unresolvable="true"
    location="classpath*:/application.properties" />

这种配置下运行测试代码,日志信息中有关加载properties资源文件会有三条如下:

 Loading properties file from URL [file:/D:/workspace_sun/framework-dev/micmiu-demoweb/target/test-classes/application.properties]
 Loading properties file from URL [file:/D:/workspace_sun/framework-dev/micmiu-demoweb/target/classes/application.properties]
 Loading properties file from URL [jar:file:/D:/micmiu-test.jar!/application.properties]

由此日志信息可知:

  • 同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
  • classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载

本文介绍到此结束@Michael Sun.

目录
相关文章
|
7天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
59 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
8天前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
19天前
|
IDE Java 开发工具
还在为繁琐的配置头疼吗?一文教你如何用 Spring Boot 快速启动,让开发效率飙升,从此告别加班——打造你的首个轻量级应用!
【9月更文挑战第2天】Spring Boot 是一款基于 Spring 框架的简化开发工具包,采用“约定优于配置”的原则,帮助开发者快速创建独立的生产级应用程序。本文将指导您完成首个 Spring Boot 项目的搭建过程,包括环境配置、项目初始化、添加依赖、编写控制器及运行应用。首先需确保 JDK 版本不低于 8,并安装支持 Spring Boot 的现代 IDE,如 IntelliJ IDEA 或 Eclipse。
59 5
|
23天前
|
Java 微服务 Spring
Spring Cloud全解析:配置中心之解决configserver单点问题
但是如果该configserver挂掉了,那就无法获取最新的配置了,微服务就出现了configserver的单点问题,那么如何避免configserver单点呢?
|
1月前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
20天前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
35 0
|
20天前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
32 0
|
23天前
|
缓存 Java 数据库连接
Spring Boot 资源文件属性配置,紧跟技术热点,为你的应用注入灵动活力!
【8月更文挑战第29天】在Spring Boot开发中,资源文件属性配置至关重要,它让开发者能灵活定制应用行为而不改动代码,极大提升了可维护性和扩展性。Spring Boot支持多种配置文件类型,如`application.properties`和`application.yml`,分别位于项目的resources目录下。`.properties`文件采用键值对形式,而`yml`文件则具有更清晰的层次结构,适合复杂配置。此外,Spring Boot还支持占位符引用和其他外部来源的属性值,便于不同环境下覆盖默认配置。通过合理配置,应用能快速适应各种环境与需求变化。
27 0
|
30天前
|
Java 数据库连接 数据库
Spring Data JPA 与 Hibernate 之区别
【8月更文挑战第21天】
17 0
|
1月前
|
Java Spring
Spring Boot Admin 授权配置
Spring Boot Admin 授权配置
26 0