In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.