Reputation: 2016
I wonder, in Spring Boot these profiles (test, dev, prod) are kinda predefined? If so, where can I see the exact settings for them? Documentation is silent about it. The reason I feel them to be predefined is a strange behavior when I set my profiles in 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
This all works fine, as I expect, but once I erase test from this line, no logging occurs.
spring.profiles.active=h2
This variation also works fine:
spring.profiles.active=dev, h2
Why is that? Profiles "test" and "dev" are not mine for sure) Thank you.
EDIT
I am doing this test:
@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));
}
}
Upvotes: 4
Views: 12614
Reputation: 2016
OK, Boot has the possibility to define profile-specific logging in logback-spring.xml
<springProfile name="dev, test">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
The tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the element. Use the name attribute to specify which profile accepts the configuration. doc
Even with file location in main/resources still affects test configuration defined for a given profile in a dedicated test application-{profile}.properties located in test/resources. Practically, if I do not have any logging setting for a current test profile, in logback-spring.xml, I cannot expect logging settings defined in properties file to override it or add. Again, either not documented or not on the surface and I simply failed to find in Spring Boot documentation.
I had this file with similar sections, where I defined loggers for certain profiles: test and dev and prod. But I never created these profiles manually. Seemingly, Boot crates them automatically.. or does some other magic known only to itself, never documented.
So what I did to resolve is to make profiles name defined in logback-spring.xml match those defined in application-{profile}.properties, putting all the logic to represent logging to xml-file. Namely:
<springProfile name="h2">
</springProfile>
matches
application-h2.properties
making properties files free from logging logic.
Hope it could help someone.
Upvotes: 1
Reputation: 1593
If you load a specific profile which does not exist, Spring falls back to the default profile and loads the values from the application.properties
file.
In your case (spring.profiles.active=dev, h2
) Spring could not find the dev profile, loads the values from the application.properties
and the application-h2.properties
So if you now load only the h2
profile spring loads only the values from the application-h2.properties
file.
Upvotes: 2