关键词:
目录
1.1 默认名称的配置文件application.properties
1.1.2 @ConfigurationProperties注解注入属性到bean
1.1.3 @Value与@ConfigurationProperties结合使用
1.2 非默认名称的配置文件config/mail.properties
1.2.1、@PropertySource注解加载properties文件
1.2.2、@PropertySource注解加载yml文件
springboot提供了多个注解可以将外部的值动态注入到Bean中,最常用的包括@Value注解,@ConfigurationProperties注解。这些注解只能读取默认的配置文件application.properties,要想加载非默认配置文件,需结合@PropertySource使用。
1、properties配置文件
1.1 默认名称的配置文件application.properties
spring.jdbc.username=admin
spring.jdbc.password=pwd123
...
1.1.1 @Value注解注入属性到bean
@Component
public class JdbcConfig
@Value("$spring.jdbc.username")
private String username;
@Value("$spring.jdbc.password")
private String password;
//省略getter与setter方法...
1.1.2 @ConfigurationProperties注解注入属性到bean
@Component
@ConfigurationProperties(prefix = "spring.jdbc")
public class JdbcConfig
private String username;
private String password;
//省略getter与setter方法...
1.1.3 @Value与@ConfigurationProperties结合使用
假如在配置文件中还有一个spring.name属性,则可以按如下形式使用。
@Component
@ConfigurationProperties(prefix = "spring.jdbc")
public class JdbcConfig
private String username;
private String password;
@Value("$spring.name")
private String name;
//省略getter与setter方法...
1.2 非默认名称的配置文件config/mail.properties
mail.host=smtp.qq.com
mail.port=587
...
1.2.1、@PropertySource注解加载properties文件
PropertySource注解的作用是加载指定的属性文件,配置属性如下@PropertySource(value= "classpath:config/mail.properties",ignoreResourceNotFound=false,encoding="UTF-8",name="mail.properties"),其中value是设置需要加载的属性文件,可以一次性加载多个(多个时以,分隔); encoding用于指定读取属性文件所使用的编码,我们通常使用的是UTF-8;ignoreResourceNotFound含义是当指定的配置文件不存在是否报错,默认是false;如这里配置的config/mail.properties,若在classpath路径下不存在时,则ignoreResourceNotFound为true的时候,程序不会报错,如果ignoreResourceNotFound为false的时候,程序直接报错。实际项目开发中,最好设置ignoreResourceNotFound为false,该参数默认值为false。name的值我们设置的是mail.properties。这个值在Springboot的环境中必须是唯一的,如果不设置,则值为:“class path resource [config/mail.properties]“。
@Component
@ConfigurationProperties(prefix = "mail")
@PropertySource(value = "classpath:config/mail.properties",encoding = "UTF-8")
public class MailConfig
private String host;
private String port;
//省略getter与setter方法...
1.2.2、@PropertySource注解加载yml文件
在1.2.1中,若配置文件为config/mail.yml时,配置属性不会注入到MailConfig的bean中。查看源代码发现,@PropertySource不支持 yml 文件的解析,该注解有个factory 这个属性,作为解析资源文件的工厂类,默认实现是 DefaultPropertySourceFactory,只需要实现能解析yml的工厂类即可以解决该问题。
2、yml配置文件支持
2.1 yml配置文件 config/mail.yml
mail:
host: smtp.qq.com
port: 587
2.2 解析yml文件的工厂类
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 java.io.IOException;
import java.util.Properties;
/**
* PropertySource支持yml配置文件
*
* @Author kongzi
* @Version 1.0
*/
public class YamlPropertySourceFactory implements PropertySourceFactory
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties ymlProperties = factory.getObject();
String propertyName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(propertyName, ymlProperties);
2.3 @PropertySource注解加载yml文件
@Component
@ConfigurationProperties(prefix = "mail")
@PropertySource(value = "classpath:config/mail.yml", factory = YamlPropertySourceFactory.class)
public class MailConfig
private String host;
private String port;
//省略getter与setter方法...
3、@value注解其他形式
@Value注解实现以下几种情况:
(1)注入普通字符;
(2)注入操作系统属性;
(3)注入表达式运算结果;
(4)注入其他Bean的属性;
(5)注入文件内容;
(6)注入网址内容;
(7)注入属性文件。
package com.kongzi;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
/**
* 配置类
**/
@Configuration
@ComponentScan("com.kongzi")
@PropertySource("classpath:db.properties")
public class ElConfig
/**
* 注入普通字符串
*/
@Value("您好,欢迎访问 carefree 的博客")
private String comment;
/**
* 注入操作系统属性
*/
@Value("#systemProperties['os.name']")
private String osName;
/**
* 注入表达式运算结果
*/
@Value("# T(java.lang.Math).random() * 100.0 ")
private double randomNumber;
/**
* 注入其他Bean的属性
*/
@Value("#otherUser.userName")
private String fromUserName;
@Value("#otherUser.blogUrl")
private String fromBlogUrl;
/**
* 注入文件资源
*/
@Value("classpath:info.txt")
private Resource testFile;
/**
* 注入网址资源
*/
@Value("https://blog.csdn.net/carefree31441")
private Resource testUrl;
/**
* 注入配置文件
*/
@Value("$jdbc.driver")
private String jdbc_driver;
@Value("$jdbc.url")
private String jdbc_url;
@Value("$jdbc.username")
private String jdbc_username;
@Value("$jdbc.password")
private String jdbc_password;
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer()
return new PropertySourcesPlaceholderConfigurer();
public void outputResource()
try
System.out.println("注入普通字符串:");
System.out.println(comment);
System.out.println("------------------------------------------------");
System.out.println("注入操作系统属性:");
System.out.println(osName);
System.out.println("------------------------------------------------");
System.out.println("注入表达式运算结果:");
System.out.println(randomNumber);
System.out.println("------------------------------------------------");
System.out.println("注入其他Bean的属性:");
System.out.println("用户名称:" + fromUserName);
System.out.println("博客地址:"+ fromBlogUrl);
System.out.println("------------------------------------------------");
System.out.println("注入文件资源:");
System.out.println("文件中的内容:" + IOUtils.toString(testFile.getInputStream()));
System.out.println("------------------------------------------------");
System.out.println("注入配置文件(方式一):");
System.out.println("数据库驱动:" + jdbc_driver);
System.out.println("数据库连接:" + jdbc_url);
System.out.println("数据库用户:" + jdbc_username);
System.out.println("数据库密码:" + jdbc_password);
System.out.println("------------------------------------------------");
System.out.println("注入配置文件(方式二):");
System.out.println("数据库驱动:" + environment.getProperty("jdbc.driver"));
System.out.println("数据库连接:" + environment.getProperty("jdbc.url"));
System.out.println("数据库用户:" + environment.getProperty("jdbc.username"));
System.out.println("数据库密码:" + environment.getProperty("jdbc.password"));
System.out.println("------------------------------------------------");
catch (Exception ex)
ex.printStackTrace();
springboot加载配置文件
在springboot启动的过程中,默契情况下会在classpath路径下加载application.properties当做系统配置文件,但有时候我们想要替换成另一个文件,可以通过以下方式: 一、在启动类里配置默认的properties文件,如下图代码 二、 ... 查看详情
springboot配置文件注解
参考技术Aspringboot使用一个全局配置文件,配置文件的名字是固定的:application.properties或application.yml@ConfigurationProperties:将全局配置文件的属性值,映射到SpringBoot组件上@Value:从全局配置文件中读取属性,映射到组件上@PropertySour... 查看详情
springboot的自定义配置
参考技术ASpringBoot免除了项目中大部分的手动配置,对一些特定情况,我们可以通过修改全局配置文件以适应具体生产环境,可以说,几乎所有的配置都可以写在application.properties文件中,SpringBoot会自动加载全局配置文件,从而... 查看详情
springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取:类上添加@ConfigurationProperties注解,prefix为yml中配置的属性名称,要想属性类生效得加上@Component注解如果想要在yml中... 查看详情
springboot源码之属性文件加载原理剖析(代码片段)
SpringBoot源码之属性文件加载原理剖析 首先我们来看一个问题。就是我们在创建SpringBoot项目的时候会在对应的application.properties或者application.yml文件中添加对应的属性信息,我们的问题是这些属性文件是什么时候被加载的&... 查看详情
springboot中propertysource注解的使用
摘要:本文重点讲解一下Spring中@PropertySource注解的使用,如何通过PropertySource注解加载指定的配置文件。以及PropertySource注解与@ConfigurationProperties两个注解的配合使用。1.1. PropertySource注解加载指定的属性文件Spring框... 查看详情
springboot属性加载顺序
SpringBoot属性加载顺序SpringBoot为了能够更合理的重写各属性的值,使用了下面这种较为特别的属性加载顺序命令行中传入的参数。SPRING_APPLICATION_JSON中的属性。SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中的内容。java:c... 查看详情
springboot自定义配置文件数量是变化的,属性相同,只是值不一样,怎样绑定到实体类?
我的springboot项目有一个需求:1.创建自定义配置文件并设置属性和值2.自定义配置文件数量会根据生成环境增减,但文件中的属性不变,只是值不一样网上许多加载配置文件并绑定实体类的案例,都是需要@PropertySource指定具体的... 查看详情
源码透视springboot的spi机制(代码片段)
一、从java类加载机制说起BootstrapClassLoader:负责加载JDK自带的rt.jar包中的类文件,是所有类加载的父类ExtensionClassLoader:负责加载java的扩展类库从jre/lib/ect目录或者java.ext.dirs系统属性指定的目录下加载类,是SystemClassLoader的父类... 查看详情
springboot-属性直接注入
SpringBoot-属性直接注入SpringBoot-属性直接注入上面我们说到,如果公共的属性,我们可以使用Java类加载Properties文件,来达到复用的目的,在SpringBoot中,我们提供了更为简单的方法,即直接使用配置文件注入。使用直接注入我们... 查看详情
springboot之加载自定义配置文件
SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定。 JavaBean:packageorg.springboot.model;importlombok.Data;importorg.springframework.boot.context.pr 查看详情
为什么springboot的资源优先级忽略了我的外部属性文件?
作为SpringBoot项目的一部分,我需要加载某些属性文件,默认情况下,该文件位于desrc/main/resources目录下。此外,我需要能够加载外部属性文件(位于项目的根目录)。如果存在此外部文件,则应将文件路径作为命令行属性传递。... 查看详情
springboot自动装配原理@enableautoconfiguration
参考技术A1、springboot启动会加载大量的自动配置类:(在下面的spring.factories文件中)2、通过@ConditionalOnXXX判断我们是否导入了相关的功能(就是pom文件中的starter),如果导入了,就会自动配置。4、给容器中添加自动配置类的时... 查看详情
springboot情操陶冶-@configurationproperties注解解析
承接前文springboot情操陶冶[email protected]注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用@ConfigurationProperties此注解多用于对配置文件的加载以及映射对应值至相应的java属性中,样例如下1.配置属性指定applica... 查看详情
springboot—配置文件详解:properties和yaml(代码片段)
一、配置文件优先级加载机制 SpringBoot项目启动会扫描以下位置的application.properties或者application.yml作为默认的配置文件 1、工程根目录:./config/ 2、工程根目录:./ 3、classpath:/config/ 4、classpat... 查看详情
springboot属性加载顺序
SpringBoot属性加载顺序SpringBoot为了能够更合理的重写各属性的值,使用了下面这种较为特别的属性加载顺序命令行中传入的参数。SPRING_APPLICATION_JSON中的属性。SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中的内容。java:c... 查看详情
使用springboot创建分层属性文件
我们有一个包含三个资源文件的项目-application-dev.propertiesapplication-test.properties和application-prod.properties。我们有一个环境变量来设置我们所处的环境的名称,然后我们使用计算的占位符加载正确的属性文件,如下所示:@Configuration@... 查看详情
springboot源码分析之bootstrap.properties文件加载的原理
SpringBoot源码分析之bootstrap.properties文件加载的原理 对于SpringBoot中的属性文件相信大家在工作中用的是比较多的,对于application.properties和application.yml文件应该非常熟悉,但是对于bootstrap.properties文件和bootstrap.yml这个两... 查看详情