Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of spring profile

Shulou Source: shulou.com Published: 2022-06-03 16:49:57 10月11日 Update

This article shares with you the content of the sample analysis of spring profile. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Background

Spring's profile is used by everyone to fly ~

So how to use the combination of profile?

For example, we use it like this.

@ Profile ({"prod", "unit-test"})

Analysis.

There should be no doubt about the above profile. It will only take effect when the profile is prod or unit-test.

But what if we use "no" ~ how to make sure it doesn't work in some cases!

Spring provides common! To describe it.

So if you want to work in a non-production environment, you just have to simply write

@ Profile ({"! prod"})

So how does it not work in multiple environments?

Some smart people [I] have the following code

@ Profile ({"! prod", "! unit-test"})

So is this the actual situation?

Let's take a look at the corresponding code

Code

Profile is controlled by profileCondition.

Class ProfileCondition implements Condition {@ Override public boolean matches (ConditionContext context, AnnotatedTypeMetadata metadata) {if (context.getEnvironment ()! = null) {MultiValueMap attrs = metadata.getAllAnnotationAttributes (Profile.class.getName ()); if (attrs! = null) {for (Object value: attrs.get ("value")) {if (context.getEnvironment (). AcceptsProfiles ((String [] value) {return true;} return false }} return true;}}

You can obviously see acceptsProfiles.

/ * * Return whether one or more of the given profiles is active or, in the case of no * explicit active profiles, whether one or more of the given profiles is included in * the set of default profiles. If a profile begins with'!' The logic is inverted, * i.e. The method will return true if the given profile is not active. * For example, env.acceptsProfiles ("p1", "! p2") will * return {@ code true} if profile 'p1' is active or' p2' is not active. * @ throws IllegalArgumentException if called with zero arguments * or if any profile is {@ code null}, empty or whitespace-only * @ see # getActiveProfiles * @ see # getDefaultProfiles * / boolean acceptsProfiles (String... Profiles)

From the above, we can see that it should be the condition of or.

Of course, the code is as follows

@ Overridepublic boolean acceptsProfiles (String... Profiles) {Assert.notEmpty (profiles, "Must specify at least one profile"); for (String profile: profiles) {if (StringUtils.hasLength (profile) & & profile.charAt (0) = ='!') {if (! isProfileActive (profile.substring (1)) {return true;}} else if (isProfileActive (profile)) {return true;}} return false;}

So you can see when it is! The condition will judge that if the profile is not currently activated, return true, otherwise the current condition is normal, change the current profile, if activated, return true, and return false if the above conditions are not met.

So the above logic tells us that it should be the logic of or. therefore

@ Profile ({"! prod", "! unit-test"})

! prod | |! unit-test=== >! (prod&&unit-test) that is, it will not be registered when both prod and unit-test take effect. All other tunes will be registered.

Thank you for reading! This is the end of this article on "sample Analysis of spring profile". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Code condition analysis time example content situation more activation environment article logic not bad practical obvious smart that is to say multiple actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS OPPO Reno NVidia Apple