gpt4 book ai didi

java - Spring Boot : spring. profile.active=dev/test/prod

转载 作者:行者123 更新时间:2023-12-02 11:01:58 25 4
gpt4 key购买 nike

我想知道,在 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

应用程序-h2.属性:

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

正如我所料,这一切都工作正常,但是一旦我从这一行删除测试,就不会发生任何日志记录。

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));
}
}

最佳答案

如果加载不存在的特定配置文件,Spring 将回退到默认配置文件并加载 application.properties 文件中的值。

在您的情况下(spring.profiles.active=dev, h2)Spring找不到开发配置文件,从application.properties加载值application-h2.properties

因此,如果您现在仅加载 h2 配置文件,则 spring 仅加载 application-h2.properties 文件中的值。

关于java - Spring Boot : spring. profile.active=dev/test/prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com