Java Spring引导:Spring.profiles.active=dev/test/prod
我想知道,在Spring Boot中,这些配置文件(测试、开发、产品)是预定义的吗?如果是,我在哪里可以看到它们的确切设置?文档对此保持沉默。 我之所以觉得它们是预定义的,是因为我在application.properties中设置配置文件时出现了一种奇怪的行为:Java Spring引导:Spring.profiles.active=dev/test/prod,java,spring,Java,Spring,我想知道,在Spring Boot中,这些配置文件(测试、开发、产品)是预定义的吗?如果是,我在哪里可以看到它们的确切设置?文档对此保持沉默。 我之所以觉得它们是预定义的,是因为我在application.properties中设置配置文件时出现了一种奇怪的行为: spring.profiles.active=test, h2 spring.jpa.hibernate.ddl-auto = none #LOGGING logging.level.root=ERROR logging.leve
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
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
应用-h2.2特性:
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
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找不到dev配置文件,从application.properties
和application-h2.properties
因此,如果您现在只加载
h2
概要文件,spring将只加载application-h2.properties
文件中的值。OK,Boot可以在logback-spring.xml中定义特定于概要文件的日志记录
<springProfile name="dev, test">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
使属性文件不受日志逻辑的影响
希望它能帮助别人。没有预定义的配置文件或设置。从SpringBoot2.1开始,我相信有一个测试配置文件可用于测试,它可能会启用一些额外的日志记录。如果这个测试配置文件静默存在,我在哪里可以看到它?我在Spring Boot 2.0.2上,没有配置文件。。。关于在运行测试时启用
测试
配置文件,以便在提供应用程序测试.properties
时自动加载,而无需自己启用测试配置文件(针对每个测试)。解决了这个问题,我在项目资源文件夹中定义了logback-spring.xml,不在测试资源文件夹中。它以某种方式影响了我的测试…如果我将所有日志记录设置放在h2配置文件中,仍然没有结果,根本没有日志记录(使用spring.profiles.active=h2)!但是,当我添加spring.profiles.active=test时,h2日志记录神奇地发生了。您是否尝试启动应用程序并使用JVM属性加载概要文件?是的,我尝试了,没有结果。我实际上在做测试,我编辑了文章并包含了测试用例。解决了这个问题,我在项目资源文件夹中定义了logback-spring.xml,而不是在测试资源文件夹中。它不知怎么影响了我的考试。。。