Spring Boot: spring.profiles.active=dev/test/prod

3

我想知道,在Spring Boot中这些配置文件(test、dev、prod)是否已经被预定义了?如果是的话,我在哪里可以看到它们的确切设置?文档对此没有说明。

我认为它们是预定义的原因是当我在application.properties中设置我的配置文件时出现了奇怪的行为:

spring.profiles.active=test, h2

spring.jpa.hibernate.ddl-auto = none

#LOGGING
logging.level.root=ERROR
logging.level.org.springframework.jdbc.datasource=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate=INFO
logging.level.org.hibernate.stat=DEBUG

application-h2.properties:

spring.datasource.url=jdbc:h2:mem:myProject;DB_CLOSE_DELAY=-1
spring.datasource.username=rat
spring.datasource.password=8965yUe4
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

spring.datasource.initialization-mode=embedded

这一切都很好地运作,正如我所期望的那样。但是当我从这行代码中删除了“test”后,就不会再记录日志了。

spring.profiles.active=h2

这个变体也能正常工作:

spring.profiles.active=dev, h2

为什么呢?“test”和“dev”配置文件肯定不属于我。谢谢。
编辑:
我正在进行以下测试:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class UserRepositoryTest {

    @Autowired
    private Environment environment;

    @Autowired
    private UserRepository userRepository;

    @Test
    public void findAllTest() throws Exception {
        final String[] activeProfiles = environment.getActiveProfiles();
        System.out.println("Profiles of mine ::");
        for (String activeProfile : activeProfiles) {
            System.out.println(activeProfile);
        }
        Assert.assertTrue(!userRepository.findAll().isEmpty());
        System.out.println(userRepository.findById(1L));
    }
}

2
没有预定义的配置文件或设置。 我认为,从Spring Boot 2.1开始,有一个可用于测试的测试配置文件,可以启用一些额外的日志记录。 - M. Deinum
如果测试配置文件存在但未被显示,我该在哪里查看它?我使用的是Spring Boot 2.0.2。 - Andreas Gelever
1
没有配置文件... 关于在运行测试时启用test配置文件进行讨论,这样当提供application-test.properties时,它将自动加载,无需为每个测试手动启用测试配置文件。 - M. Deinum
问题解决了,我在项目的资源文件夹中定义了logback-spring.xml,而不是在测试资源文件夹中。这不知何故影响了我的测试... - Andreas Gelever
我认为有预定义的配置文件,例如:Config Server 中的 native。引用: 2.1.3 文件系统后端 在 Config Server 中还有一个“本地”配置文件, 它不使用 Git, 而是从本地类路径或文件系统加载配置文件 (您想要指向的任何静态 URL 都可以使用 spring.cloud.config.server.native.searchLocations)。 要使用本地配置文件,请使用 spring.profiles.active=native 启动 Config Server。 https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html - Nor.Z
2个回答

2
如果您加载了一个不存在的特定配置文件,Spring会回退到默认配置文件并从application.properties文件中读取值。
在您的情况下(spring.profiles.active=dev, h2),Spring无法找到dev配置文件,因此从application.propertiesapplication-h2.properties文件中读取值。
如果您现在仅加载h2配置文件,则Spring仅从application-h2.properties文件中读取值。

如果我将所有日志设置都放在h2配置文件中,仍然没有结果,根本没有日志记录(使用spring.profiles.active=h2)!但是,只要我添加spring.profiles.active=test,h2日志记录就会神奇地出现。 - Andreas Gelever
你尝试过使用JVM属性启动应用程序并加载配置文件吗? - smsnheck
是的,我已经这样做了,但没有结果。 我正在进行测试,我编辑了帖子并包含了测试用例。 - Andreas Gelever
已解决,我在项目资源文件夹中定义了logback-spring.xml,而不是在测试资源文件夹中定义。这在某种程度上影响了我的测试... - Andreas Gelever

1

好的,Boot有可能在logback-spring.xml中定义特定于配置文件的日志记录

<springProfile name="dev, test">
    <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

标签允许您根据活动的Spring配置文件选择性地包含或排除配置部分。配置文件部分可以在 任何位置支持元素。使用name属性指定哪个配置文件接受配置。doc

即使在main/resources中有文件位置,仍会影响针对专用于测试的test/resources中的应用程序-{profile}.properties中定义的给定配置文件的测试配置。实际上,如果我没有当前测试配置文件的任何日志设置,则不能指望在属性文件中定义的日志设置覆盖它或添加它。同样,要么未记录在案,要么不表面,并且我只是在Spring Boot文档中找不到。

我有一个类似的文件,其中定义了某些配置文件的记录器:test和dev和prod。但我从未手动创建这些配置文件。看来,Boot会自动创建它们...或者进行其他仅自己知道的魔法,从未记录。

因此,我所做的解决方法是使logback-spring.xml中定义的配置文件名称与application-{profile}.properties中定义的名称匹配,将表示日志记录的所有逻辑放入xml文件中。即:

<springProfile name="h2">
</springProfile>

匹配

application-h2.properties

使属性文件摆脱日志逻辑。
希望它能帮助某些人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接