在Spring Boot中使用Profiles配置文件

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

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

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

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

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

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

  1. server.port = 8180

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

  1. server.port = 8080

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

  1. $ ./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"。

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

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

一个复杂的Spring Boot Profile

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

  1. 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数据源。

  1. 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文件包含两个配置文件的定义,每一个都有不同的属性。

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

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

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

在一个Bean上使用@Profile

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

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

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

  1. @SpringBootApplication
  2. public class DemoMain {
  3. public static void main(String[] args) {
  4. System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "prod");
  5. SpringApplication sa = new SpringApplication(DemoMain.class);
  6. sa.run(args);
  7. }
  8. }

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

  1. @Configuration
  2. public class SampleApplicationInitializer implements WebApplicationInitializer {
  3. @Override
  4. public void onStartup(ServletContext servletContext) throws ServletException {
  5. servletContext.setInitParameter("spring.profiles.active", "prof");
  6. }
  7. }

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

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

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

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

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

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

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

  1. $ export spring_profiles_active=prod

相关文章