在Spring Boot中使用Profiles配置文件

x33g5p2x  于2022-10-04 转载在 Spring  
字(3.0k)|赞(0)|评价(0)|浏览(508)

本教程将简要介绍Spring Boot配置文件以及它们如何使你的应用程序更加灵活。

Spring Boot配置文件使你能够根据代码部署的环境来灵活配置。例如,与生产或预生产环境相比,开发环境可能需要不同的应用设置。

让我们看一个实际的例子。在下面的项目结构中,我们定义了以下属性文件。

$ tree src/main/resources/  src/main/resources/ ├── application-dev.properties ├── application-prod.properties

第一个文件-application-dev.properties-包含一个Web服务器端口设置。

server.port = 8180

另一方面,另一个文件包含以下设置。

server.port = 8080

选择配置文件的最简单方法是将-Dspring.profiles.active=传递给JVM。 例如。

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=prod   .   ____          _            __ _ _  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\__, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v2.1.1.RELEASE)  2018-12-04 17:27:50.362  INFO 9969 --- [           main] com.example.demomvc.DemoMvcApplication   : Starting DemoMvcApplication on localhost.localdomain with PID 9969 (/home/francesco/springboot/demo-mvc/target/classes started by francesco in /home/francesco/springboot/demo-mvc) 2018-12-04 17:27:50.367  INFO 9969 --- [           main] com.example.demomvc.DemoMvcApplication   : The following profiles are active: prod

正如你从日志中看到的,活动的配置文件是 "prod"。

与此类似,你可以用以下方式运行你的例子。

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=dev

一个复杂的Spring Boot Profile

在现实世界中,你会使用Profiles来定义你的数据库设置,这些设置通常在开发、生产或测试环境中是不同的。所以你可以有一个application-dev.properties,其中配置了一个H2数据源。

spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 spring.datasource.username=sa spring.datasource.password=sa

另外,你可以有一个application-production.properties,其中配置了一个Oracle数据源。

spring.datasource.url= jdbc:oracle:thin:@//192.168.10.1:1521/ORCL spring.datasource.username=system spring.datasource.password=manager spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

在一个配置文件中声明多个配置文件

也可以在一个配置文件中声明多个配置文件。例如,下面的application.yml文件包含两个配置文件的定义,每一个都有不同的属性。

spring:   profiles: dev server:   port: 8000
spring:   profiles: prod server:   port: 9000

同样地,你可以通过传递JVM启动参数来运行一个或另一个配置文件。

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=prod

在一个Bean上使用@Profile

Profiles也可以通过**@Profile**注解在Bean中声明。在这种情况下,只要你使用注解中指定的Profile,Bean就可以使用。

@Component @Profile("dev") public class MyBean

如果你正在寻找一种程序化的方式来选择Profile,那么你也可以这样做! 考虑一下下面这个应用类。

@SpringBootApplication
public class DemoMain {
  public static void main(String[] args) {
    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "prod");
    SpringApplication sa = new SpringApplication(DemoMain.class);
    sa.run(args);
  }
}

然后,值得一提的是,Web启动类能够通过ServletContext来设置活动配置文件。

@Configuration
public class SampleApplicationInitializer implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("spring.profiles.active", "prof");
  }
}

最后,你也可以直接在环境中设置配置文件。

@Autowired private ConfigurableEnvironment env;
env.setActiveProfiles("prod");

###在XML中声明配置文件

配置文件也可以在旧的XML配置文件中配置。

<?xml version="1.0" encoding="UTF-8"?><beans profile="dev">
        
   <bean id="myBean" class="com.example.MyBean"/>
    
</beans>

使用环境变量设置配置文件

在Linux环境下,配置文件也可以通过环境变量激活。

$ export spring_profiles_active=prod

相关文章