Spring IOC Pratap Kumar - 2
Spring IOC Pratap Kumar - 2
• 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.
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");
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.
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);
PropertySource
PropertySource objects.
• A PropertySource is a simple abstraction over any source of
@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.
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.
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.
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