Spring Boot 通过@PropertySource或者@PropertySources实现设置多配置文件
Spring Boot 官网使用的是application.properties文件来实现文件的配置。但是实际情况下一个配置文件是不够用的,比如项目集成redis,mq,以及数据库比如mysql的时候,多个配置文件有利于开发及维护的管理。Spring Boot是通过@PropertySource或者@PropertySources来实现多配置文件的。首先看下@PropertySource源码:
public @interface PropertySource {
/**
* Indicate the name of this property source. If omitted, a name will
* be generated based on the description of the underlying resource.
* @see org.springframework.core.env.PropertySource#getName()
* @see org.springframework.core.io.Resource#getDescription()
*/
String name() default "";
/**
* Indicate the resource location(s) of the properties file to be loaded.
* <p>Both traditional and XML-based properties file formats are supported
* — for example, {@code "classpath:/com/myco/app.properties"}
* or {@code "file:/path/to/file.xml"}.
* <p>Resource location wildcards (e.g. **/*.properties) are not permitted;
* each location must evaluate to exactly one {@code .properties} resource.
* <p>${...} placeholders will be resolved against any/all property sources already
* registered with the {@code Environment}. See {@linkplain PropertySource above}
* for examples.
* <p>Each location will be added to the enclosing {@code Environment} as its own
* property source, and in the order declared.
*/
String[] value();
/**
* Indicate if failure to find the a {@link #value() property resource} should be
* ignored.
* <p>{@code true} is appropriate if the properties file is completely optional.
* Default is {@code false}.
* @since 4.0
*/
boolean ignoreResourceNotFound() default false;
/**
* A specific character encoding for the given resources, e.g. "UTF-8".
* @since 4.3
*/
String encoding() default "";
/**
* Specify a custom {@link PropertySourceFactory}, if any.
* <p>By default, a default factory for standard resource files will be used.
* @since 4.3
* @see org.springframework.core.io.support.DefaultPropertySourceFactory
* @see org.springframework.core.io.support.ResourcePropertySource
*/
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
name
为这里资源指定一个名称,这个没什么好说的。
value
用于指定资源路径,注意通配符(比如/*.properties)在这里是没有用的,路径必须明确指向到一个properties文件。因为这里value的类型是String数组,因此这里可以指定多个配置文件。
ignoreResourceNotFound
是否忽略找不到指定路径的情况。
encoding
指定编码类型,默认为空。
通过@PropertySource源码解析我们就能够知道应该如何使用该注解。这里假设需要多配置两个配置文件:redis.properties和database.properties:
只需要在启动类上加上@PropertySource即可:
@SpringBootApplication
@ComponentScan(basePackages = {"com.aron"})//通过扫描本路径可不需将ctl包和启动类放在同一目录下
@PropertySource(value= {"classpath:redis.properties","classpath:database.properties"}
, name="ss"
, encoding="utf-8"
,ignoreResourceNotFound=true)
public class ProjectMainEntranceApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectMainEntranceApplication.class, args);
}
}
而对于@PropertySources 来说,参照其源码:
public @interface PropertySources {
PropertySource[] value();
}
我们可以看到其实就是PropertySource的数组,因此通过@PropertySources 配置方式为:
@SpringBootApplication
@ComponentScan(basePackages = { "com.aron" }) // 通过扫描本路径可不需将ctl包和启动类放在同一目录下
@PropertySources({ @PropertySource("classpath:redis.properties"),
@PropertySource("classpath:database.properties") })
public class ProjectMainEntranceApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectMainEntranceApplication.class, args);
}
}