Spring Boot Profiles 实现多环境配置
1.单个yml文件
创建一个名为 application.yml的文件
server:
port: 8080
my:
name: demo
spring:
profiles:
active: dev
---
#development environment
spring:
profiles: dev
server:
port: 8160
my:
name: ricky
---
#test environment
spring:
profiles: test
server:
port: 8180
my:
name: test
---
#production environment
spring:
profiles: prod
server:
port: 8190
my:
name: prod
application.yml文件分为四部分,使用 --- 来作为分隔符,
第一部分通用配置部分,表示三个环境都通用的属性,
后面三段分别为:开发,测试,生产,用spring.profiles指定了一个值(开发为dev,测试为test,生产为prod),这个值表示该段配置应该用在哪个profile里面。
如果是本地启动,在通用配置里面可以设置调用哪个环境的profile,也就是第一段的spring.profiles.active=XXX, 其中XXX是后面3段中spring.profiles对应的value,通过这个就可以控制本地启动调用哪个环境的配置文件
注意:如果spring.profiles.active没有指定值,那么只会使用没有指定spring.profiles文件的值,也就是只会加载通用的配置。
启动参数
如果是部署到服务器的话,我们正常打成jar包,启动时通过 --spring.profiles.active=xxx
来控制加载哪个环境的配置,完整命令如下:
java -jar xxx.jar --spring.profiles.active=test 表示使用测试环境的配置
2.多个yml文件配置属性
将于环境无关的属性放置到application.yml文件里面;通过与配置文件相同的命名规范,创建application-{profile}.yml文件 存放不同环境特有的配置,例如 application-test.yml 存放测试环境特有的配置属性,application-prod.yml 存放生产环境特有的配置属性。
通过这种形式来配置多个环境的属性文件,在application.yml文件里面spring.profiles.active=xxx来指定加载不同环境的配置,如果不指定,则默认只使用application.yml属性文件,不会加载其他的profiles的配置
二、Maven Profile
通过Maven的profile特性来实现多环境配置打包
pom.xml配置如下:
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources.${build.profile.id}</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
通过执行 mvn clean package -P ${profile}
来指定使用哪个profile
@profile使用方法
@profile:加载指定配置文件时才起作用。只有当Profile指定的配置被激活的时候,才会将Profile中所对应的Bean注册到Spring容器中
@Configuration
@EnableSwagger2
@Profile( "dev")
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.web"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("xxxxx")
.termsOfServiceUrl("http://xxxxxxx/")
.version("1.0").build();
}
}
只有项目加载dev的配置时以上代码才会起作用
Environment使用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Autowired
private Environment environment;
@Value("${my.name}")
private String myName;
public void test(){
System.out.println(myName);
System.out.println(environment.getProperty("my.name"));
}
}
@ConditionalOnExpression
作用:特定条件下生效
@Configuration
@ConditionalOnExpression("${test.enabled:true}")
public class TestConfiguration {
@Bean
public TestBean testBean() {
return new TestBean();
}
}
这个bean只有配置文件在test.enabled: true
的时候才会进行初始化
@Configuration,@Bean,@Component
@Configuration 相当于<beans>
可以用在类,接口(包括注解类型),或者枚举声明上
@Bean 相当于<bean>
,这个只能用在方法或者注解类型声明上
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
从定义来看, @Configuration 注解本质上还是 @Component,因此 <context:component-scan/> 或者 @ComponentScan 都能处理@Configuration 注解的类。
@Configuration 标记的类必须符合下面的要求:
配置类必须以类的形式提供(不能是工厂方法返回的实例),允许通过生成子类在运行时增强(cglib 动态代理)。
配置类不能是 final 类(没法动态代理)。
配置注解通常为了通过 @Bean 注解生成 Spring 容器管理的类,
配置类必须是非本地的(即不能在方法中声明,不能是 private)。
任何嵌套配置类都必须声明为static。
@Bean 方法可能不会反过来创建进一步的配置类(也就是返回的 bean 如果带有 @Configuration,也不会被特殊处理,只会作为普通的 bean)