襄阳市建设公司网站网站建设用处
news/
2025/10/2 6:03:30/
文章来源:
襄阳市建设公司网站,网站建设用处,哈尔滨seo优化代理,做门户网站价格文章目录 配置文件位置和路径自定义配置文件 属性注入添加yaml文件的支持 配置文件
位置和路径
当我们创建一个 Spring Boot 工程时#xff0c;默认 resources 目录下就有一个 application.properties 文件#xff0c;可以在 application.properties 文件中进行项目配置默认 resources 目录下就有一个 application.properties 文件可以在 application.properties 文件中进行项目配置但是这个文件并非唯一的配置文件在 Spring Boot 中一共有 4 个地方可以存放 application.properties 文件。按照下面的顺序四个配置文件的优先级依次降低
当前项目根目录下的 config 目录下: config/application.properties当前项目的根目录下application.propertiesresources 目录下的 config 目录下src/main/resources/config/application.propertiesresources 目录下 src/main/resources/application.properties 推荐
这四个位置是默认位置即 Spring Boot 启动默认会从这四个位置按顺序去查找相关属性并加载。 yaml作为配置文件和properties是一致的。
自定义配置文件 spring.config.name 指定配置文件名默认的配置文件名是application可以使用spring.config.name指定自定义文件名如下示例 java -jar myproject.jar --spring.config.namemyprojectspring.profiles.active 激活指定的配置文件application-{profile}.properties指定的配置文件要存放在和application.properties相同的目录 系统默认加载application-[default].properties配置文件;使用逗号分隔多个profile配置文件;在application配置文件中可以指定待激活的配置文件示例 # 系统会按照顺序加载application-dev.properties、application-test.properties配置文件后面的配置文件会覆盖前面同名属性配置
spring.profiles.activedev,testspring.config.location 通过 spring.config.location属性来手动的指定配置文件位置指定完成后系统就会自动去指定目录下查找application.properties 文件。 多个配置使用逗号分隔如果指定的是目录要用/结尾 # 系统就会自动去指定目录下查找application.properties或application.yml文件注意路径以 / 结尾
java -jar properties-0.0.1-SNAPSHOT.jar --spring.config.locationclasspath:/xingmu/
# 如果指定多个配置文件注意以逗号分割要特别注意的是该命令指定的配置文件会使项目默认的application.properties或application.yml文件失效换句话说该命令会用指定的配置文件替换application.properties或application.yml文件。 spring.config.additional-location 该命令用于追加配置文件。原有的application.properties或application.yml文件均有效。用于和原有配置进行合并 # 系统就会自动去指定目录下查找application.properties或application.yml文件注意路径以 / 结尾
java -jar properties-0.0.1-SNAPSHOT.jar --spring.config.additional-locationclasspath:/xingmu/spring.profiles.include spring.profiles.active和spring.profiles.include的使用与区别 指定包含哪些特定配置文件spring.profiles.active用来指定激活指定的配置文件而spring.profiles.include可以用来指定激活配置文件还包含哪些配置文件如默认配置文件application.properties server.port8003
# test、prod、publish
spring.profiles.activedev被激活的配置文件是application-dev.properties: spring.profiles.includedevDb,devRedis可以用来指定不同环境之间的切换及不同种类配置的加载。
属性注入
在 resources 下面新建一个 book.properties 文件内容如下
book.name三国演义
book.author罗贯中
book.id1
book.tags小说,演义,历史项目启动并不会自动的加载 book.properties 该配置文件如果是在 XML 配置中可以通过如下方式引用该 properties 文件 context:property-placeholder locationclasspath:book.properties/在 Java 配置中可以通过 PropertySource 来引入配置这样当项目启动时就会自动加载 book.properties 文件。这只是 Spring 中属性注入的一个简单用法和 Spring Boot 没有任何关系。 PropertySource默认默认情况下仅仅支持加载外部的、后缀为properties配置文件不支持yml、yaml文件。 注意 在 application.properties 文件中定义属性按照传统的方式Spring中的方式可以直接通过 Value 注解将这些属性注入到Book 对象中 注意: Book 对象本身也要交给 Spring 容器去管理如果 Book 没有交给 Spring 容器那么 Book 中的属性也无法从 Spring 容器中获取到值。 // classpath:后面不能带有空格
Component
PropertySource(classpath:book.properties)
public class Book {Value(${book.id})private Long id;Value(${book.name})private String name;Value(${book.author})private String author;Value(${book.tags})private String[] tags;//省略getter/setter
}在 Spring Boot 引入了类型安全的属性注入如果采用 Spring 中的配置方式当配置的属性非常多的时候工作量就很大了而且容易出错。使用类型安全的属性注入可以有效的解决这个问题。 示例代码如下 Component
PropertySource(classpath:book.properties)
ConfigurationProperties(prefix book)
public class Book {private Long id;private String name;private String author;private String[] tags;//省略getter/setter
}添加yaml文件的支持
新建YamlSourceFactory 实现PropertySourceFactory 接口。
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;public class YamlSourceFactory implements PropertySourceFactory {Overridepublic PropertySource? createPropertySource(Nullable String name, EncodedResource resource) throws IOException {Properties propertiesFromYaml loadYamlIntoProperties(resource);String sourceName name ! null ? name : resource.getResource().getFilename();return new PropertiesPropertySource(sourceName, propertiesFromYaml);}private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {try {YamlPropertiesFactoryBean factory new YamlPropertiesFactoryBean();factory.setResources(resource.getResource());factory.afterPropertiesSet();return factory.getObject();} catch (IllegalStateException e) {// for ignoreResourceNotFoundThrowable cause e.getCause();if (cause instanceof FileNotFoundException) {throw (FileNotFoundException) e.getCause();}throw e;}}}
使用 Component
PropertySource(value classpath:book.yml, factory YamlSourceFactory.class)
ConfigurationProperties(prefix book)
public class Book {private Long id;private String name;private String author;private String[] tags;//省略getter/setter
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/924592.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!