environment是什么呢....中文是环境大家都知道但是具体代表什么呢?感觉很抽象....从代码里的解释来看environment代表了profile和properties.

profile就是1组bean的定义.实际用途就是在不同环境比如测试环境和生产环境中加载不同的bean达到根据环境加载bean的用途.(因为测试环境可能有些bean是模拟的,比如接口.调用返回的报文都是自己模拟的,真实的bean在测试环境拿不到).

properties就不用说了.就是配置...

这篇文章我想分享下我对properties的学习,因为profile我感觉只要会配置就行了...可能更偏向于配置运维,而properties主要涉及到类型转化等比较复杂的东西.而我自己也写过一些conveter.所以想特别学习下.

 

结构

Spring 学习记录2 Environment

大概就是这样的结构.java web环境下一般都是StandardServletEnvironment环境,而我自己做junit测试的时候是StandardEnvironment 这里主要分析我对StandardEnvironment 的学习(子类可能也就增加了一点点其他功能吧.总的来说应该都是大同小异.估计是把servlet的环境变量也加到properties里了.)

environment的profile的功能是定义在Environment类里的

 1 public interface Environment extends PropertyResolver {
 2 
 3     /**
 4      * Return the set of profiles explicitly made active for this environment. Profiles
 5      * are used for creating logical groupings of bean definitions to be registered
 6      * conditionally, for example based on deployment environment.  Profiles can be
 7      * activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
 8      * "spring.profiles.active"} as a system property or by calling
 9      * {@link ConfigurableEnvironment#setActiveProfiles(String...)}.
10      * <p>If no profiles have explicitly been specified as active, then any {@linkplain
11      * #getDefaultProfiles() default profiles} will automatically be activated.
12      * @see #getDefaultProfiles
13      * @see ConfigurableEnvironment#setActiveProfiles
14      * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
15      */
16     String[] getActiveProfiles();
17 
18     /**
19      * Return the set of profiles to be active by default when no active profiles have
20      * been set explicitly.
21      * @see #getActiveProfiles
22      * @see ConfigurableEnvironment#setDefaultProfiles
23      * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME
24      */
25     String[] getDefaultProfiles();
26 
27     /**
28      * Return whether one or more of the given profiles is active or, in the case of no
29      * explicit active profiles, whether one or more of the given profiles is included in
30      * the set of default profiles. If a profile begins with '!' the logic is inverted,
31      * i.e. the method will return true if the given profile is <em>not</em> active.
32      * For example, <pre class="code">env.acceptsProfiles("p1", "!p2")</pre> will
33      * return {@code true} if profile 'p1' is active or 'p2' is not active.
34      * @throws IllegalArgumentException if called with zero arguments
35      * or if any profile is {@code null}, empty or whitespace-only
36      * @see #getActiveProfiles
37      * @see #getDefaultProfiles
38      */
39     boolean acceptsProfiles(String... profiles);
40 
41 }
View Code

相关文章:

  • 2021-07-03
  • 2021-06-17
  • 2021-06-05
  • 2022-01-30
  • 2022-01-04
  • 2021-10-12
  • 2021-12-23
  • 2021-11-12
猜你喜欢
  • 2021-07-10
  • 2021-12-07
  • 2021-09-11
  • 2021-07-14
  • 2021-10-13
相关资源
相似解决方案