paulschwarz / spring-dotenv

Provides a Dotenv property source for Spring
MIT License
301 stars 23 forks source link

spring.profiles.active cannot be set #5

Closed aBnormaLz closed 3 years ago

aBnormaLz commented 3 years ago

Seems Spring doesn't load the proper application.properties file according to the profile. After the application started it succesfully overrides the spring.profiles.active so the following variable's value is dev in runtime

    @Value("${spring.profiles.active}")
    private String profile;

You can see the log here, hope it helps

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-07-24 13:49:42.337 TRACE 4366 --- [           main] m.p.springdotenv.DotenvPropertySource    : DotenvPropertySource add to Environment
2020-07-24 13:49:42.341  INFO 4366 --- [           main] com.example.test.ExampleApplication      : Starting ExampleApplication on AlexMacBookPro.local with PID 4366 (/Users/sukeinalex/development/example/target/classes started by sukeinalex in /Users/sukeinalex/development/example)
2020-07-24 13:49:42.342  INFO 4366 --- [           main] com.example.test.ExampleApplication      : The following profiles are active: ${env.SPRING_PROFILES_ACTIVE}
2020-07-24 13:49:42.469  WARN 4366 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-07-24 13:49:42.923  INFO 4366 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-07-24 13:49:42.996  INFO 4366 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 65ms. Found 3 JPA repository interfaces.
2020-07-24 13:49:43.489  INFO 4366 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-07-24 13:49:43.497  INFO 4366 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-24 13:49:43.497  INFO 4366 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-24 13:49:43.562  INFO 4366 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-24 13:49:43.562  INFO 4366 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1191 ms
2020-07-24 13:49:43.654  INFO 4366 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.4.4 by Redgate
2020-07-24 13:49:43.659  INFO 4366 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-07-24 13:49:43.971  INFO 4366 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-07-24 13:49:43.994  INFO 4366 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://localhost:3306/example (MySQL 5.7)
2020-07-24 13:49:44.059  INFO 4366 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 5 migrations (execution time 00:00.024s)
2020-07-24 13:49:44.070  INFO 4366 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `example`: 1.4
2020-07-24 13:49:44.071  INFO 4366 --- [           main] o.f.core.internal.command.DbMigrate      : Schema `example` is up to date. No migration necessary.
2020-07-24 13:49:44.115  INFO 4366 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-24 13:49:44.122  INFO 4366 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-07-24 13:49:44.152  INFO 4366 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-07-24 13:49:44.189 TRACE 4366 --- [           main] m.p.springdotenv.DotenvPropertySource    : Getting env property for 'env.SPRING_PROFILES_ACTIVE'
2020-07-24 13:49:44.192 TRACE 4366 --- [           main] m.p.springdotenv.DotenvPropertySource    : Getting env property for 'env.EXAMPLE_NAME'
2020-07-24 13:49:44.193  INFO 4366 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.17.Final
2020-07-24 13:49:44.246  WARN 4366 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-07-24 13:49:44.307  INFO 4366 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-07-24 13:49:44.402  INFO 4366 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
2020-07-24 13:49:44.546  INFO 4366 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-24 13:49:44.547  INFO 4366 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-07-24 13:49:44.881  INFO 4366 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-07-24 13:49:44.887  INFO 4366 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-07-24 13:49:45.160  INFO 4366 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-07-24 13:49:45.169  INFO 4366 --- [           main] com.example.test.ExampleApplication      : Started ExampleApplication in 3.136 seconds (JVM running for 3.613)
paulschwarz commented 3 years ago

Hi @aBnormaLz

Firstly, the concept of .env in general is to abandon "profiles", please read the second last paragraph of https://12factor.net/config

Having said that, I spun up an example and verified that profiles are working correctly. I was able to run my Spring Boot application with a "dev" profile and have an application-dev.properties file which was correctly picked up as one would expect.

I see this in your logs m.p.springdotenv.DotenvPropertySource : Getting env property for 'env.SPRING_PROFILES_ACTIVE' This is not the correct usage of the .env pattern.

Feel free to reopen this thread if you'd like to discuss it further.