I want my default active profile to be production
if -Dspring.profiles.active
is not set.
I tried the following in my application.properties
but it did't work:
spring.profiles.default=production
Spring-boot version = 1.3.5.RELEASE
This question is related to
java
spring
spring-boot
Currently using Maven + Spring Boot. Our solution was the following:
application.properties
#spring.profiles.active= # comment_out or remove
securityConfig.java
@Value(${spring.profiles.active:[default_profile_name]}")
private String ACTIVE_PROFILE_NAME;
Credit starts with MartinMlima. Similar answer provided here:
How do you get current active/default Environment profile programmatically in Spring?
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
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:
[email protected]@
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.
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.
If you're using maven I would do something like this:
Being production your default profile:
<properties>
<activeProfile>production</activeProfile>
</properties>
And as an example of other profiles:
<profiles>
<!--Your default profile... selected if none specified-->
<profile>
<id>production</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activeProfile>production</activeProfile>
</properties>
</profile>
<!--Profile 2-->
<profile>
<id>development</id>
<properties>
<activeProfile>development</activeProfile>
</properties>
</profile>
<!--Profile 3-->
<profile>
<id>otherprofile</id>
<properties>
<activeProfile>otherprofile</activeProfile>
</properties>
</profile>
<profiles>
In your application.properties you'll have to set:
spring.profiles.active=@activeProfile@
This works for me every time, hope it solves your problem.
add --spring.profiles.active=production
Example:
java -jar file.jar --spring.profiles.active=production
Try this:
@PropertySource("classpath:${spring.profiles.active:production}_file.properties")
In AWS LAMBDA:
For $ sam local
you add the following line in your sam template yml file:
Resources:
FunctionName:
Properties:
Environment:
Variables:
SPRING_PROFILES_ACTIVE: local
But in AWS Console: in your Lambda Environment variables just add:
KEY:JAVA_TOOL_OPTIONS
VALUE:-Dspring.profiles.active=dev
you can also have multiple listings in the @Profile annotation
@Profile({"dev","default"})
If you set "default" as an additional value, you don't have to specify spring.profiles.active
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
If you are using AWS Lambda with SprintBoot, then you must declare the following under environment variables:
key: JAVA_TOOL_OPTIONS & value: -Dspring.profiles.active=dev
I do it this way
System.setProperty("spring.profiles.default", "dev");
in the very beginning of main(...)
Source: Stackoverflow.com