Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Use Spring Profiles to Make Your Life Easier

Sign upSign InSign upSign InMember-only storyDave TaublerFollowLevel Up Coding--ShareSpring Profiles are a core — and very useful — feature of the Spring Framework. Many Java projects make use of them to load different config values for different environments (e.g. dev, stage, and production). In reality, they can be used for so much more. To that end, it can be tempting for engineers to overuse them.We’ll discuss all of that in this article.Let’s start by describing what Spring Profiles are. According to the Spring Framework’s own documentation:Spring Profiles provide a way to segregate parts of your Application configuration and make it only available in certain environments.In other words, we can define environments in which our application can run, and configure our application such that some parts are loaded only when the app is running in specific environments.Probably the most common such environments — and the ones that likely have immediate lept to mind here—are those that describe the various stages in the deployment process; e.g.However, as we’ll shortly discuss, there are many more ways by which we can segment parts of our application. For this reason, I often use the analogy of flavors rather than environments.How do we define a profile? From a practical standpoint, defining a Profile is as simple as providing an argument to our application startup script. Using the java command, for example, we would provide a spring.profiles.active argument. So if we wanted to define a dev profile, we would run the following:java -Dspring.profiles.active=dev -jar myapp.jarThis is probably the most common way to set a profile under which our application will run. But there are other ways. For example, prior to running our application, we can set a spring_profiles_active environment variable:export spring_profiles_active=devWe can also set the profile as a context param in our web.xml file:----Level Up CodingSoftware architect, engineering leader, musician, husband, dadDave TaublerinLevel Up Coding--5Arslan MirzainLevel Up Coding--5Jacob BennettinLevel Up Coding--67Dave TaublerinDataDrivenInvestor--6JP Brown--304Paul Mason--39Love SharmainDev Genius--31Michael LonginBetter Programming--19Dominik PolzerinTowards Data Science--8Yael Wolfe--96HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Use Spring Profiles to Make Your Life Easier

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×