
Play Store Application link โ Spring Boot in 13 steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Profiles_and_Environment_Management
Step 1: Setting Up a Spring Boot Project
Using Spring Initializr
- Go to https://start.spring.io/.
- Fill in the details:
- Project: Maven
- Language: Java
- Spring Boot: 3.x.x
- Dependencies: Spring Web
- Click Generate, and extract the downloaded ZIP file.
Import into IDE
- Open your IDE (e.g., IntelliJ or Eclipse).
- Import the extracted project as a Maven project.
Step 2: Using Profiles in Spring Boot
Spring Boot allows you to manage different configurations using profiles.
Create Profile-Specific Property Files
- application.properties (Default configuration):
server.port=8080
app.message=Default Profile
- application-dev.properties (Development configuration):
server.port=8081
app.message=Development Profile
- application-prod.properties (Production configuration):
server.port=8082
app.message=Production Profile
Activate a Profile
Profiles can be activated via:
- application.properties:
spring.profiles.active=dev
- Command Line:
java -Dspring.profiles.active=prod -jar demo-0.0.1-SNAPSHOT.jar
- Environment Variables:
Set the SPRING_PROFILES_ACTIVE
variable in your operating system or deployment environment.
Step 3: Access Profile-Specific Properties
Example: Display Active Profile
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
class ProfileController {
@Value("${app.message}")
private String message;
@GetMapping("/profile")
public String getProfileMessage() {
return "Active Profile Message: " + message;
}
}
Run the application and access http://localhost:<port>/profile
to see the message based on the active profile.
Step 4: Customizing Configurations with @ConfigurationProperties
Example: Create a Custom Configuration Class
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Add Configuration to Properties Files
- application-dev.properties:
app.name=Demo App
app.description=Development Configuration
- application-prod.properties:
app.name=Demo App
app.description=Production Configuration
Access the Configuration in a Controller
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ConfigController {
private final AppConfig appConfig;
public ConfigController(AppConfig appConfig) {
this.appConfig = appConfig;
}
@GetMapping("/config")
public String getConfig() {
return "App Name: " + appConfig.getName() + ", Description: " + appConfig.getDescription();
}
}
Run the application and access http://localhost:<port>/config
to see the profile-specific configuration.
Step 5: Using @Value for Inline Property Injection
Example: Inject Individual Properties
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ValueController {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDescription;
@GetMapping("/value")
public String getValueProperties() {
return "App Name: " + appName + ", Description: " + appDescription;
}
}
Conclusion
In this guide, we:
- Used profiles for environment-specific configurations.
- Activated profiles via multiple methods.
- Customized configurations using
@ConfigurationProperties
and@Value
.
I just like the helpful info you supply for your articles. Iๆฃl bookmark your blog and check again right here regularly. I’m reasonably certain I will learn many new stuff proper right here! Best of luck for the next!
One other issue is that if you are in a situation where you do not possess a co-signer then you may really need to try to wear out all of your financing options. You can get many grants or loans and other scholarship grants that will offer you finances to help you with institution expenses. Thank you for the post.
In these days of austerity as well as relative stress about taking on debt, some people balk resistant to the idea of having a credit card to make purchase of merchandise and also pay for a holiday, preferring, instead just to rely on the tried as well as trusted way of making repayment – raw cash. However, if you possess the cash available to make the purchase fully, then, paradoxically, that is the best time just to be able to use the credit card for several motives.
Hi there very cool website!! Man .. Excellent .. Amazing .. I’ll bookmark your website and take the feeds additionally I am glad to find numerous useful information right here in the publish, we want work out more techniques in this regard, thanks for sharing. . . . . .
Thanks a lot for sharing this with all of us you actually know what you’re talking about! Bookmarked. Kindly also visit my site =). We could have a link exchange contract between us!
https://www.youtube.com/channel/UCyt2dGrKTf9KpBk1jdUl3oA
Thank you for your help and this post. Itโs been great. http://www.hairstylesvip.com