Spring中使用classpath加载配置文件浅析

合集下载

spring配置文件各个属性详解

spring配置文件各个属性详解

spring配置文件各个属性详解分类:spring 2012-08-09 11:25 9316人阅读评论(2) 收藏举报springaophibernateattributesxhtmlwebsphere目录(?)[+]一、引用外部属性文件<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:mail.properties</value><value>classpath:jdbc.properties</value></list></property></bean>我们定义了一个PropertyPlaceholderConfigurer类的实例,并将其位置属性设置为我们的属性文件。

该类被实现为Bean工厂的后处理器,并将使用定义在文件中的属性来代替所有的占位符(${...}value)。

注意:而在spring2.5的版本中提供了一种更简便的方式,如:1.<context:property-placeholderlocation="classpath:config/jdbc.properties"/>这样以后要使用属性文件中的资源时,可以使用${属性名}来获得。

二、常用数据源的配置第一种是:DBCP数据源,(需要加入2个jar文件,在spring中的lib下jakarta-commons/commons-dbcp.jar和commons-pools.jar)主要配置如下:<!-- Mysql版--><bean id="dataSource"class="mons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url"value="${jdbc.url}"></property><property name="username" value="${ername}"></property><property name="password" value="${jdbc.password}"></property></bean>第二种是:c3p0数据源,跟第一种一个类型,需加入c3p0.jar包。

[spring]配置文件分析

[spring]配置文件分析

[spring]配置⽂件分析配置⽂件默认的配置⽂件名为application.properties;数据格式为:key = properties配置⽂件⽬录路径说明1. file:./config/⼯程⽂件夹下的config⽬录2. file:/⼯程⽂件夹⽬录,如果是maven项⽬那么和pom.xml是⼀个层级3. classPath:/config/⼯程 classPath⽂件夹下的config⽂件夹4. classPath:/⼯程classpath⽂件夹除了上述的⽂件⽬录以外,还可以⽤spring.config.location 参数的形式指定配置⽂件,spring.profiles.activ 参数指定不同环境的配置⽂件,命令如下:java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.propertiesjava -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/conf/app.propertiesjava -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod如果上述的默认路径有配置⽂件的话,会和配置内容会形成互补作⽤,相同的内容优先级别⾼的会覆盖优先级别低的。

application.properties相关操作通过${key}⽅式获取其他key中的value,例如配置⽂件中:name1=zhangsan name2=${name1}获取配置⽂件⽅式a)通过上下⽂对象.getEnvironment().getProperty("key")获取b) 通过@Value("${key}")注解,将配置⽂件中的value注⼊到类中;也可以通过@Value("value")⽅式直接给属性赋值c) 通过@Configuration和@PropertySource注解来添加配置⽂件d) 注⼊配置⽂件前缀,如果配置⽂件中key的格式和注⼊配置⽂件前缀获取value操作如下:。

SpringBoot配置文件——加载顺序

SpringBoot配置文件——加载顺序

SpringBoot配置⽂件——加载顺序⼀、存放⽬录Application属性⽂件,按优先级排序,位置⾼的将覆盖位置1. 当前项⽬⽬录下的⼀个/config⼦⽬录2. 当前项⽬⽬录3. 项⽬的resources即⼀个classpath下的/config包4. 项⽬的resources即classpath根路径(root)如图:⽬录⼆、读取顺序如果在不同的⽬录中存在多个配置⽂件,它的读取顺序是:1、config/application.properties(项⽬根⽬录中config⽬录下)2、config/application.yml3、application.properties(项⽬根⽬录下)4、application.yml5、resources/config/application.properties(项⽬resources⽬录中config⽬录下)6、resources/config/application.yml7、resources/application.properties(项⽬的resources⽬录下)8、resources/application.yml顺序可以通过实验验证:1~8 个位置分别定义不同的 server 端⼝号 9001~9008即可验证结果顺序注:1、如果同⼀个⽬录下,有application.yml也有application.properties,默认先读取application.properties。

2、如果同⼀个配置属性,在多个配置⽂件都配置了,默认使⽤第1个读取到的,后⾯读取的不覆盖前⾯读取到的。

3、创建SpringBoot项⽬时,⼀般的配置⽂件放置在项⽬的resources⽬录下,因为配置⽂件的修改,通过热部署不⽤重新启动项⽬,⽽热部署的作⽤范围是classpath下三、配置⽂件的⽣效顺序,会对值进⾏覆盖1. @TestPropertySource 注解2. 命令⾏参数3. Java系统属性(System.getProperties())4. 操作系统环境变量5. 只有在random.*⾥包含的属性会产⽣⼀个RandomValuePropertySource6. 在打包的jar外的应⽤程序配置⽂件(application.properties,包含YAML和profile变量)7. 在打包的jar内的应⽤程序配置⽂件(application.properties,包含YAML和profile变量)8. 在@Configuration类上的@PropertySource注解9. 默认属性(使⽤SpringApplication.setDefaultProperties指定)四、配置随机值roncoo.secret={random.value} roncoo.number={random.int}roncoo.bignumber={random.long} roncoo.number.less.than.ten={random.int(10)}roncoo.number.in.range=${random.int[1024,65536]}读取使⽤注解:@Value(value = "${roncoo.secret}")注:出现黄点提⽰,是要提⽰配置元数据,可以不配置五、属性占位符当application.properties⾥的值被使⽤时,它们会被存在的Environment过滤,所以你能够引⽤先前定义的值(⽐如,系统属性)=roncoo.desc = ${} is a domain nameYou can automatically expand properties from the Maven project by using resource filtering. If you use the spring-boot-starter-parent, you can then refer to your Maven ‘project properties’ with @..@ placeholders, as shown in the following example:app.encoding=@project.build.sourceEncoding@app.java.version=@java.version@六、其他配置的介绍端⼝配置server.port=8090时间格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss时区设置spring.jackson.time-zone=Asia/Chongqing。

SpringBoot框架中的配置文件加载

SpringBoot框架中的配置文件加载

SpringBoot框架中的配置文件加载在SpringBoot框架中,配置文件是非常重要的一部分,用于配置应用程序的各种参数和属性。

SpringBoot提供了多种方式来加载配置文件,以满足不同的需求和场景。

一、默认的配置文件加载在SpringBoot中,默认会加载名为"application.properties"或"application.yml"的配置文件。

这些文件可以放置在以下几个位置:1. 项目根目录下的"config"文件夹;2. 项目根目录下;3. classpath下的"config"文件夹;4. classpath下。

SpringBoot会按照上述顺序依次搜索并加载配置文件。

如果存在多个同名的配置文件,后加载的配置文件会覆盖前者的配置。

二、自定义的配置文件加载除了默认的配置文件加载方式,SpringBoot还支持自定义的配置文件加载。

我们可以通过使用@PropertySource注解来指定要加载的配置文件。

例如,我们可以创建一个名为"custom.properties"的配置文件,并在SpringBoot的主类中使用@PropertySource注解加载该文件:```java@Configuration@PropertySource("classpath:custom.properties")public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}```这样,SpringBoot会加载并解析"custom.properties"文件中的配置内容,并将其注入到应用程序中使用。

Spring注解@Value及属性加载配置文件方式

Spring注解@Value及属性加载配置文件方式

Spring注解@Value及属性加载配置⽂件⽅式Spring中使⽤@Value注解给bean加载属性的配置⽂件有两种使⽤⽅式第⼀种:使⽤@Value("#{configProperties['websit.msgname']}")spring中配置属性加载⽂件的配置⽅式<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:/properties/websit.properties</value></list></property></bean>注意1.这⾥使⽤的configProperties必须要和定义的bean名称⼀致。

2.websit⽤来指定msgname来源于那个配置⽂件3.配置的加载属性bean名称为org.springframework.beans.factory.config.PropertiesFactoryBean第⼆种:使⽤@Value("${websit.msgname}");使⽤这种⽅式,⼜可以有两种配置⽅式⽅式⼀<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="properties" ref="configProperties"/></bean><bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:/properties/websit.properties</value></list></property></bean>⽅式⼆<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:properties/websit.properties</value></list></property></bean>当使⽤@Value注解bean属性时,如果没有在配置⽂件中配置,这时启动spring就会抛出异常。

Spring中加载xml配置文件的几种方式

Spring中加载xml配置文件的几种方式

项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,就总结了下Spring中加载xml配置文件的方式, xml是最常见的spring 应用系统配置源。

Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext,.....一: XmlBeanFactory 引用资源1.Resource cr = new ClassPathResource("applicationContext.xml");BeanFactory bf=new XmlBeanFactory(cr);UserDao userDao = (UserDao)bf.getBean("userDao");二: ClassPathXmlApplicationContext 编译路径使用ClassPathXmlApplicationContext对象获取,必须把applicationContext.xml放置到类的加载路径中,也就是Src下面1.ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");// src目录下的2.ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao");3.ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext-oracle.xml","applicationContext.xml"});UserDao userDao = (UserDao)context.getBean("userDao");// src/conf 目录下的4.ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");5.ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");三: FileSystemXmlApplicationContext用文件系统的路径必须把applicationContext.xml放置到工程目录下面,也就是项目路径的下面1.ApplicationContext factory=newFileSystemXmlApplicationContext("src/appcontext.xml");//使用了classpath: 前缀,作为标志, 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径没有classpath的话就是从当前的工作目录2.ApplicationContext factory=newFileSystemXmlApplicationContext("classpath:appcontext.xml");3.ApplicationContext factory=newFileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");4.ApplicationContext factory=newFileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");四: XmlWebApplicationContext是专为Web工程定制的。

[转]spring配置文件中classpath路径解析

[转]spring配置文件中classpath路径解析

[转]spring配置⽂件中classpath路径解析
src不是classpath, WEB-INF/classes,lib才是classpath
WEB-INF/ 是资源⽬录, 客户端不能直接访问,
这话是没错,不过现在的IDE编译器在编译时会把src下的⽂件(是⽂件,不是.java)移到WEB-INF/classes下。

不过值得注意的是,spring 配置⽂件⾥这个locations是uri表⽰,也就是说你写的jdbc.properties是当前相对路径,要访问classpath记得要这样写:
<value>classpath:jdbc.properties</value>
⾸先 classpath是指 WEB-INF⽂件夹下的classes⽬录
解释classes含义:
1.存放各种资源配置⽂件 eg.init.properties log4j.properties struts.xml
2.存放模板⽂件 eg.actionerror.ftl
3.存放class⽂件对应的是项⽬开发时的src⽬录编译⽂件
总结:这是⼀个定位资源的⼊⼝
如果你知道开发过程中有这么⼀句话:惯例⼤于配置那么也许你会改变你的想法
对于第⼆个问题
这个涉及的是lib和classes下⽂件访问优先级的问题: lib>classes
对于性能的影响应该不在这个范畴
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找⽂件;
classpath*:不仅包含class路径,还包括jar⽂件中(class路径)进⾏查找.。

Spring之ClassPathResource加载资源文件

Spring之ClassPathResource加载资源文件

Spring之ClassPathResource加载资源⽂件先看Demo:1@Test2public void testClassPathResource() throws IOException {3 Resource res = new ClassPathResource("resource/ApplicationContext.xml");4 InputStream input = res.getInputStream();5 Assert.assertNotNull(input);6 }再看内部源码:1public ClassPathResource(String path) {2this(path, (ClassLoader) null);3 }1public ClassPathResource(String path, ClassLoader classLoader) {2 Assert.notNull(path, "Path must not be null");3 String pathToUse = StringUtils.cleanPath(path);4if (pathToUse.startsWith("/")) {5 pathToUse = pathToUse.substring(1);6 }7this.path = pathToUse;8this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());9 }1public ClassPathResource(String path, Class<?> clazz) {2 Assert.notNull(path, "Path must not be null");3this.path = StringUtils.cleanPath(path);4this.clazz = clazz;5 }获取资源内容:1/**2 * This implementation opens an InputStream for the given class path resource.3 * @see ng.ClassLoader#getResourceAsStream(String)4 * @see ng.Class#getResourceAsStream(String)5*/6@Override7public InputStream getInputStream() throws IOException {8 InputStream is;9if (this.clazz != null) {10 is = this.clazz.getResourceAsStream(this.path);11 }12else if (this.classLoader != null) {13 is = this.classLoader.getResourceAsStream(this.path);14 }15else {16 is = ClassLoader.getSystemResourceAsStream(this.path);17 }18if (is == null) {19throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");20 }21return is;22 }源码解读:该类获取资源的⽅式有两种:Class获取和ClassLoader获取。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Spring中使用classpath加载配置文件浅析在应用Spring的工程中,使用class path的方式加载配置文件应该是最常用的做法,然而对大部分人来说,刚开始使用Spring时,几乎都碰到过加载配置文件失败的情况,除了配置上的错误外,很多时候是因为配置文件的路径和程序中指定的加载路径不一致,从而导致配置文件找不到,或是加载了错误地方的配置文件。

本文将就Spring如何从class path中加载配置文件做一些简要的分析。

情形一:使用classpath加载且不含通配符这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource 方法获取资源的URL,如果无法获得当前线程的ClassLoader,Spring将使用加载类org.springframework.util.ClassUtils的ClassLoader。

1.当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,这时使用ApplicationContext context =new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,Spring将加载bin/conf目录下的application-context.xml 文件。

Spring启动时的输出显示为:Loading XML bean definitions fromclass path resource [conf/application-context.xml]2.当工程目录结构如图所示:即bin目录下只有.class文件,没有配置文件,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:这时使用ApplicationContext context =new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,Spring将加载conf.jar文件中conf目录下的application-context.xml文件。

Spring启动时的输出显示为:Loading XML bean definitions fromclass path resource [conf/application-context.xml]3. 当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:这时使用ApplicationContext context =new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,由于没有使用classpath*前缀,Spring只会加载一个application-context.xml文件。

在eclipse中将会加载bin/conf目录下的application-context.xml文件,而jar包中的conf/application-context.xml并不会被加载,Spring启动时的输出显示为:Loading XML bean definitions fromclass path resource [conf/application-context.xml]情形二:使用classpath加载,包含通配符碰到通配符的情况时,Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置,结合下面给出的几种情况可以更好地理解Spring的这种工作方式1. 当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,这时使用ApplicationContext context = newClassPathXmlApplicationContext("conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,即bin/conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml文件和bin/conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\application-context.xml]2.当工程目录结构如图所示:即bin目录下只有.class文件,没有配置文件,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:这时使用ApplicationContext context = newClassPathXmlApplicationContext("conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf 的路径,即conf.jar中的conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此conf/application-context.xml文件和conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:Loading XML bean definitions from class path resource[conf/admin/admin-application-context.xml]Loading XML bean definitions from class path resource[conf/application-context.xml]3.当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:这时使用ApplicationContext context = newClassPathXmlApplicationContext("conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf 的路径,在eclipse中是bin/conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml文件和bin/conf/admin/admin-application-context.xml都会被加载,但conf.jar文件中的配置文件并不会被加载,Spring启动时的输出显示为:Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\application-context.xml]情形三:使用classpath*前缀且不包含通配符使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。

当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:这时使用ApplicationContext context = newClassPathXmlApplicationContext("classpath*:conf/application-context.xml");来创建ApplicationContext对象的话, Spring将会加载bin目录下的application-context.xml 文件和jar包里的application-context.xml文件,Spring启动时的输出显示为:Loading XML bean definitions from URL[file:/D:/myworkspace/spring-study/bin/conf/application-context.xml]Loading XML bean definitions from URL[jar:file:/D:/myworkspace/conf1.jar!/conf/application-context.xml]Spring中使用classpath加载配置文件浅析(续)情形四:使用classpath*前缀,包含通配符当工程目录结构如图所示:即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries 里导入conf.jar文件,jar文件结构如图所示:ApplicationContext context = newClassPathXmlApplicationContext("classpath*:conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,由于使用了classpaht*前缀,因此bin目录下的conf和jar包里的conf都会被加载,同时由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml和bin/conf/admin/admin-application-context.xml以及jar包中的conf/application-context.xml和conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]Loading XML bean definitions from file[D:\myworkspace\spring-study\bin\conf\application-context.xml]Loading XML bean definitions from URL[jar:file:/D:/myworkspace/conf1.jar!/conf/admin/admin-application-context.xml] Loading XML bean definitions from URL[jar:file:/D:/myworkspace/conf1.jar!/conf/application-context.xml]特别注意:如果工程目录如图所示:即配置文件直接放在bin目录中,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:ApplicationContext context = newClassPathXmlApplicationContext("classpath*:**/*application-context.xml");来创建ApplicationContext对象的话,Spring只会加载bin/application-context.xml和bin/admin/admin-application-context.xml,而jar包中的配置文件并不会被加载。

相关文档
最新文档