I want my default active profile to be production
if -Dspring.profiles.active
is not set.
I tried the following in my application.pro
Put this in the App.java:
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(App.class);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
if (!source.containsProperty("spring.profiles.active") &&
!System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {
app.setAdditionalProfiles("production");
}
...
}
This is how it is done in JHipster
What you are doing here is setting the default default profile (the profile that is used on any bean if you don't specify the @Profile
annotation) to be production
.
What you actually need to do is set the default active profile, which is done like this:
spring.profiles.active=production
The neat way to do this without changing your source code each time is to use the OS environment variable SPRING_PROFILES_ACTIVE
:
export SPRING_PROFILES_ACTIVE=production
how-to-set-active-spring-profiles
We to faced similar issue while setting spring.profiles.active
in java.
This is what we figured out in the end, after trying four different ways of providing spring.profiles.active
.
In java-8
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This works fine
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
In java-11
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This doesn't works
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
NOTE: If you're specifying spring.profiles.active
in your application.properties
file then make sure you provide spring.config.location
or spring.config.additional-location
option to java accordingly as mentioned above.
First of all, with the solution below, is necessary to understand that always the spring boot will read the application.properties
file. So the other's profile files only will complement and replace the properties defined before.
Considering the follow files:
application.properties
application-qa.properties
application-prod.properties
1) Very important. The application.properties
, and just this file, must have the follow line:
spring.profiles.active=@spring.profiles.active@
2) Change what you want in the QA and PROD configuration files to see the difference between the environments.
3) By command line, start the spring boot app with any of this options:
It will start the app with the default application.properties
file:
mvn spring-boot:run
It will load the default application.properties
file and after the application-qa.properties
file, replacing and/or complementing the default configuration:
mvn spring-boot:run -Dspring.profiles.active=qa
The same here but with the production environment instead of QA:
mvn spring-boot:run -Dspring.profiles.active=prod
One can have separate application properties files according to the environment, if Spring Boot application is being created. For example - properties file for dev environment, application-dev.properties:
spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=dev
spring.hivedatasource.password=dev
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
application-test.properties:
spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=test
spring.hivedatasource.password=test
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
And a primary application.properties file to select the profile:
application.properties:
spring.profiles.active=dev
server.tomcat.max-threads = 10
spring.application.name=sampleApp
Define the DB Configuration as below:
@Configuration
@ConfigurationProperties(prefix="spring.hivedatasource")
public class DBConfig {
@Profile("dev")
@Qualifier("hivedatasource")
@Primary
@Bean
public DataSource devHiveDataSource() {
System.out.println("DataSource bean created for Dev");
return new BasicDataSource();
}
@Profile("test")
@Qualifier("hivedatasource")
@Primary
@Bean
public DataSource testHiveDataSource() {
System.out.println("DataSource bean created for Test");
return new BasicDataSource();
}
This will automatically create the BasicDataSource bean according to the active profile set in application.properties file. Run the Spring-boot application and test.
Note that this will create an empty bean initially until getConnection() is called. Once the connection is available you can get the url, driver-class, etc. using that DataSource bean.