Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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));
    }
}
like image 697
Andreas Gelever Avatar asked Sep 15 '25 14:09

Andreas Gelever


1 Answers

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.

like image 56
smsnheck Avatar answered Sep 17 '25 06:09

smsnheck