• The Environment interface is an abstraction integrated in
the container that models two key aspects of the
                 application environment: profiles and properties.
               • Profile
                  – A profile is a named, logical group of bean definitions to be
                    registered with the container only if the given profile is
                    active. Beans may be assigned to a profile whether defined
                    in XML or with annotations.
Environment       – The role of the Environment object with relation to profiles is
 Abstraction
                    in determining which profiles (if any) are currently active,
                    and which profiles (if any) should be active by default.
                                                                                      1
          • XML Bean Definition Profiles
              – The XML counterpart is the profile attribute of the <beans> element.
          • Using @Profile
              – The @Profile annotation lets you indicate that a component is eligible for
                registration when one or more specified profiles are active.
          • Activating a Profile
              – Activating a profile can be done in several ways, but the most straightforward is
                to do it programmatically against the Environment API which is available
                through an ApplicationContext.
              – ctx.getEnvironment().setActiveProfiles("profile1", "profile2");
          • In addition, you can also declaratively activate profiles through the
            spring.profiles.active property, which may be specified through system
            environment variables, JVM system properties, servlet context parameters
Profile     in web.xml, or even as an entry in JNDI
              – -Dspring.profiles.active="profile1,profile2"
                                                                                                    2
          • Default Profile
              – You can change the name of the default profile by using setDefaultProfiles() on
                the Environment or ,declaratively, by using the spring.profiles.default property.
                PropertiesLoaderSupport ( AC )
                        PropertiesFactoryBean ( CC )
                        PropertyResoruceConfigurer ( AC ) ( BFP )
                                PlaceholderConfgurerSupport (AC) ${ … }
                                        PropertyPlaceHodlerConfigurer
                                                 PreferencesPlaceholder-
                                                Configurer
                                        PropertySourcesPlaceholderConfigurer
 Externalized                   PropertyOverrideConfigurer
Configuration
                                                                               3
                <context:property-placeholder / >
                <context:property-override/>
             • Properties play an important role in almost all applications
               and may originate from a variety of sources: properties
               files, JVM system properties, system environment
               variables, JNDI, servlet context parameters, ad-
               hoc Properties objects, Map objects, and so on.
             • The role of the Environment object with relation to
               properties is to provide the user with a convenient service
               interface for configuring property sources and resolving
               properties from them.
Properties
                                                                              4
                 • Spring’s Environment abstraction provides search operations
                   over a configurable hierarchy of property sources.
                 ApplicationContext ctx = new GenericApplicationContext();
                 Environment env = ctx.getEnvironment();
                 boolean containsMyProperty = env.containsProperty("my-
                 property");
                 System.out.println("Does my environment contain the 'my-
                 property' property? " + containsMyProperty);
                 • The Environment object performs a search over a set of
PropertySource
                   PropertySource objects.
                 • A PropertySource is a simple abstraction over any source of
   Abstraction     key-value pairs, and Spring’s StandardEnvironment is
                   configured with two PropertySource objects — one
                   representing the set of JVM system properties
                   (System.getProperties()) and one representing the set of      5
                   system environment variables (System.getenv()).
                  • The @PropertySource annotation provides a convenient and
                    declarative mechanism for adding a PropertySource to Spring’s
                    Environment.
                  @Configuration
                  @PropertySource("classpath:/com/myco/app.properties")
                  public class AppConfig {
         Using
@PropertySource
                                                                                    6
              • Java’s standard java.net.URL class and standard handlers for
                various URL prefixes, unfortunately, are not quite adequate
                enough for all access to low-level resources.
              • For example, there is no standardized URL implementation that
                may be used to access a resource that needs to be obtained
                from the classpath or relative to a ServletContext.
              • The Resource Interface
              • Built-in Resource Implementations
  Resource    • The ResourceLoader
Abstraction   • The ResourceLoaderAware interface
              • Application Contexts and Resource Paths
                                                                                7
                • The ApplicationContext interface extends an interface called
                  MessageSource and, therefore, provides internationalization
                  (“i18n”) functionality.
                • When an ApplicationContext is loaded, it automatically searches
                  for a MessageSource bean defined in the context. The bean must
                  have the name messageSource.
                • If such a bean is found, all calls to the preceding methods are
                  delegated to the message source. If no message source is found,
                  the ApplicationContext attempts to find a parent containing a bean
                  with the same name.
                • If it does, it uses that bean as the MessageSource. If the
MessageSource
                  ApplicationContext cannot find any source for messages, an empty
                  DelegatingMessageSource is instantiated in order to be able to
                  accept calls to the methods defined above.
                                                                                       8
                • Spring provides two MessageSource implementations,
                  ResourceBundleMessageSource and StaticMessageSource.
                <beans>
                 <bean id="messageSource"
                class="org.springframework.context.support.ResourceBundleMes
                sageSource">
                   <property name="basenames">
                     <list>
                       <value>format</value>
                       <value>exceptions</value>
                       <value>windows</value>
MessageSource
                     </list>
                   </property>
                 </bean>                                                       9
                </beans>
       • The Spring Expression Language (“SpEL” for short) is a
         powerful expression language that supports querying and
         manipulating an object graph at runtime.
       • The language syntax is similar to Unified EL but offers
         additional features, most notably method invocation and basic
         string templating functionality.
       • While SpEL serves as the foundation for expression evaluation
SPEL
         within the Spring portfolio, it is not directly tied to Spring and
         can be used independently.
                                                                              10
             • The SpEL classes and interfaces you are most likely to use are
               located in the org.springframework.expression package and its
               sub-packages, such as spel.support.
             ExpressionParser parser = new SpelExpressionParser();
             Expression exp = parser.parseExpression("'Hello World'");
             String message = (String) exp.getValue();
   Literal
Expression
                                                                                11
            • Relational Operators
               – The relational operators (equal, not equal, less than, less than or
                 equal, greater than, and greater than or equal) are supported by
                 using standard operator notation.
            • Logical Operators
               –    SpEL supports the following logical operators:
                    •   and or not
            • Mathematical Operators
               – You can use the addition operator on both numbers and strings.
Operators
                 You can use the subtraction, multiplication, and division operators
                 only on numbers. You can also use the modulus (%) and
                 exponential power (^) operators. Standard operator precedence is
                 enforced.                                                           12
Thank You.
             13