Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 162 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting the default active profile in Spring-boot

#1
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
Reply

#2
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.
Reply

#3
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
Reply

#4
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
Reply

#5
I do it this way

System.setProperty("spring.profiles.default", "dev");

in the very beginning of `main(...)`
Reply

#6
add `--spring.profiles.active=production`

Example:

java -jar file.jar --spring.profiles.active=production
Reply

#7
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](

[To see links please register here]

). Similar answer provided here:

[To see links please register here]

Reply

#8
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




Reply

#9
**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`

[![enter image description here][1]][1]




[1]:
Reply

#10
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.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through