Spring Core
The Core Container
The Core Container
- Core container consist of spring-core, spring-beans, spring-context, spring-context-support, and spring-expression modules.
- The spring-core and spring-beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features.
- The BeanFactory is a sophisticated implementation of the factory pattern.
- It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.
- The Context (spring-context) module builds on the solid base provided by the Core and Beans modules:
- It is a means to access objects in a framework-style manner that is similar to a JNDI registry.
- The Context module inherits its features from the Beans module and adds support for
- internationalization (using, for example, resource bundles),
- event propagation,
- resource loading, and the
- transparent creation of contexts by, for example, a Servlet container.
- The Context module also supports Java EE features such as EJB, JMX, and basic remoting.
- The ApplicationContext interface is the focal point of the Context module.
- spring-context-support provides support for integrating common third-party libraries into a Spring application context for
- caching (EhCache, Guava, JCache),
- mailing (JavaMail), scheduling (CommonJ, Quartz) and
- template engines (FreeMarker, JasperReports, Velocity).
- The spring-expression module provides a powerful Expression Language for querying and manipulating an object graph at runtime.
- It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification.
- The language supports
- setting and getting property values,
- property assignment,
- method invocation,
- accessing the content of arrays,
- collections and indexers,
- logical and arithmetic operators,
- named variables, and
- retrieval of objects by name from Spring’s IoC container.
- It also supports list projection and selection as well as common list aggregations.
Spring Beans
Spring Beans
- All classes that are used by IoC container are called Spring Beans or Beans.
- Beans can be declared in configuration file (eg: spring configuration xml) or as Annotation
- Beans declared in configuration files can be present within same file or in different file
- Beans can access to each other by specifying the bean references
- Accessing bean within the configuration file
<ref local=”beanName” />
- Not supported from Spring 4.x
- Accessing bean from different configuration file
<ref bean=”beanName” />
- Can also access bean from same file
Initializing bean properties
Initializing bean properties
- Spring initializes bean properties while instantiation of the bean using specified values
- Property value can be specified in value attribute of property tag
<property name="propName" value="propValue" />
or<property name="propName"><value>propValue</value></property>
- Property value can also be injected by using “p” schema as an attribute of bean
<bean id="beanId" class="className" p:name="Atul" p:age="21" />
- Declare
xmlns:p=”http://www.springframework.org/schema/p
in confg file
Spring bean configuration files
Spring bean configuration files
- A Spring project can have one or more bean configuration files
- Multiple spring bean configuration files helps in modularity and understandability
- Each module or component can have it’s own separate config file
- A bean configuration file can load/import any other bean configuration file within project classpath
<import resource="path/module-one.xml"/>
Spring bean scopes
Spring bean scopes
Spring Inner bean
Spring Inner bean