What is Spring?
Spring performs two major roles for a Java application.
First Spring is a container that manages all or some of the objects used by the application. Behind the scenes Spring configures your objects with what they need to in order to perform their roles in the application. If you need a Data Access Object, you ask the Spring container to provide one that is already configured with values for its data source and other properties.
Spring is also a framework because it provides libraries of classes that make it easier to accomplish common tasks such as transaction management, database integration, email, and web applications.
All Java applications that consist of multiple classes have inter-dependencies or coupling between classes. Spring helps us develop applications that minimize the negative effects of coupling and encourages the use of interfaces in application development. Using interfaces in our applications to specify type helps make our applications easier to maintain and enhance later.
The Spring framework helps developers clearly separate responsibilities. Many Java applications suffer from class bloat – that is a class that has too many responsibilities. For example a service class that is also logging information about what its doing. Think of two situations – one is you’ve been told by your supervisor to do your normal work but also to write down everything you do and how long it takes you. You’d be even busier and less responsive.
A better situation would be you do your normal work, but another person observers what you’re doing and records it and measures how long it took. Even better would be if you were totally unaware of that other person and that other person was able to also observe and record other people’s work and time.
controllers, interceptors run in the IoC container
Allows multiple DispatcherServlets that can share an “application context”
Interface based not class-based
Spring performs two major roles for a Java application.
First Spring is a container that manages all or some of the objects used by the application. Behind the scenes Spring configures your objects with what they need to in order to perform their roles in the application. If you need a Data Access Object, you ask the Spring container to provide one that is already configured with values for its data source and other properties.
Spring is also a framework because it provides libraries of classes that make it easier to accomplish common tasks such as transaction management, database integration, email, and web applications.
What does Spring provide ?
Spring is a lightweight framework. Most of your Java classes will have nothing about Spring in their source code. This means that you can easily transition your application from the Spring framework to something else. It also means that transferring an existing application to use the Spring framework doesn’t have to mean a complete code rewrite.All Java applications that consist of multiple classes have inter-dependencies or coupling between classes. Spring helps us develop applications that minimize the negative effects of coupling and encourages the use of interfaces in application development. Using interfaces in our applications to specify type helps make our applications easier to maintain and enhance later.
The Spring framework helps developers clearly separate responsibilities. Many Java applications suffer from class bloat – that is a class that has too many responsibilities. For example a service class that is also logging information about what its doing. Think of two situations – one is you’ve been told by your supervisor to do your normal work but also to write down everything you do and how long it takes you. You’d be even busier and less responsive.
A better situation would be you do your normal work, but another person observers what you’re doing and records it and measures how long it took. Even better would be if you were totally unaware of that other person and that other person was able to also observe and record other people’s work and time.
What are the modules Spring Provides ?
- The Core package is the most fundamental part of the framework and provides the IoC and Dependency Injection features. The basic concept here is the BeanFactory, which provides a sophisticated implementation of the factory pattern which removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.
- The Context package build on the solid base provided by the Core package: it provides a way to access objects in a framework-style manner in a fashion somewhat reminiscent of a JNDI-registry. The context package inherits its features from the beans package and adds support for internationalization (I18N) (using for example resource bundles), event-propagation, resource-loading, and the transparent creation of contexts by, for example, a servlet container.
- The DAO package provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old Java objects).
- The ORM package provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis. Using the ORM package you can use all those O/R-mappers in combination with all the other features Spring offers, such as the simple declarative transaction management feature mentioned previously.
- Spring's AOP package provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code implementing functionality that should logically speaking be separated. Using source-level metadata functionality you can also incorporate all kinds of behavioral information into your code, in a manner similar to that of .NET attributes.
- Spring's Web package provides basic web-oriented integration features, such as multipart file-upload functionality, the initialization of the IoC container using servlet listeners and a web-oriented application context. When using Spring together with WebWork or Struts, this is the package to integrate with.
- Spring's MVC package provides a Model-View-Controller (MVC) implementation for web-applications. Spring's MVC framework is not just any old implementation; it provides a clean separation between domain model code and web forms, and allows you to use all the other features of the Spring Framework.
- Not a J2EE container. Doesn’t compete with J2EE app servers. Simply provides alternatives.
- POJO-based, non-invasive framework which allows a la carte usage of its components.
- Promotes decoupling and reusability
- Reduces coding effort and enforces design discipline by providing out-of-box implicit pattern implementations such as singleton, factory, service locator etc.
- Removes common code issues like leaking connections and more
- Support for declarative transaction management
- Easy integration with third party tools and technologies.
- Instead of objects invoking other objects, the dependant objects are added through an external entity/container.
- Also known as the Hollywood principle – “don’t call me I will call you”
- Dependency injection
- Dependencies are “injected” by container during runtime.
- Beans define their dependencies through constructor arguments or properties
- Prevents hard-coded object creation and object/service lookup.
- Loose coupling
- Helps write effective unit tests
- The bean class is the actual implementation of the bean being described by the BeanFactory.
- Bean examples – DAO, DataSource, Transaction Manager, Persistence Managers, Service objects, etc
- Spring config contains implementation classes while your code should program to interfaces.
- Bean behaviors include:
- Singleton or prototype
- Autowiring
- Initialization and destruction methods
- init-method
- destroy-method
- Beans can be configured to have property values set.
- Can read simple values, collections, maps, references to other beans, etc.
BeanFactory is core to the Spring framework
- Lightweight container that loads bean definitions and manages your beans.
- Configured declaratively using an XML file, or files, that determine how beans can be referenced and wired together.
- Knows how to serve and manage a singleton or prototype defined bean
- Responsible for lifecycle methods.
- Injects dependencies into defined beans when served
- Removes the need for ad-hoc singletons and factories
Explain the Spring ApplicationContext ?
- A Spring ApplicationContext allows you to get access to the objects that are configured in a BeanFactory in a framework manner.
- ApplicationContext extends BeanFactory
- Adds services such as international messaging capabilities.
- Add the ability to load file resources in a generic fashion.
- Several ways to configure a context:
- XMLWebApplicationContext – Configuration for a web application.
- ClassPathXMLApplicationContext – standalone XML application context
- FileSystemXmlApplicationContext
- Allows you to avoid writing Service Locators
- Adds services such as international messaging capabilities.
- Add the ability to load file resources in a generic fashion.
- XMLWebApplicationContext – Configuration for a web application.
- ClassPathXMLApplicationContext – standalone XML application context
- FileSystemXmlApplicationContext
How Spring Support Struts?
- ContextLoaderPlugin
- Loads a Spring application context for the Struts ActionServlet.
- Struts Actions are managed as Spring beans.
- ActionSupport and DispatchActionSupport
- Pre-built convenience classes to provide access to the context.
- Provides methods in superclass for easy context lookup.
- Loads a Spring application context for the Struts ActionServlet.
- Struts Actions are managed as Spring beans.
- Pre-built convenience classes to provide access to the context.
- Provides methods in superclass for easy context lookup.
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
Explain Transaction Management in Spring ?
1. DataSourceTransactionManager - PlatformTransactionManager implementation for single JDBC data sources. Binds a JDBC connection from the specified data source to the thread, potentially allowing for one thread connection per data source.
2. HibernateTransactionManager- PlatformTransactionManager implementation for single Hibernate session factories. Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread Session per factory. SessionFactoryUtils and HibernateTemplate are aware of thread-bound Sessions and participate in such transactions automatically. Using either is required for Hibernate access code that needs to support this transaction handling mechanism.
3. JdoTransactionManager - PlatformTransactionManager implementation for single JDO persistence manager factories. Binds a JDO PersistenceManager from the specified factory to the thread, potentially allowing for one thread PersistenceManager per factory. PersistenceManagerFactoryUtils and JdoTemplate are aware of thread-bound persistence managers and take part in such transactions automatically. Using either is required for JDO access code supporting this transaction management mechanism.
4. JtaTransactionManager - PlatformTransactionManager implementation for JTA, i.e. J2EE container transactions. Can also work with a locally configured JTA implementation. This transaction manager is appropriate for handling distributed transactions, i.e. transactions that span multiple resources, and for managing transactions on a J2EE Connector (e.g. a persistence toolkit registered as JCA Connector).
Explian some of the DAO Support classes in Spring Framework ?
- JdbcDaoSupport
- Provides callback methods for row iteration
- HibernateDaoSupport
- Initializes Hibernate session factory
- Provides templates to invoke Hibernate API or the session
- SqlMapDaoSupport
- Provides template for iBatis SQLMaps
- Support similar to Hibernate template
What is Spring MVC?
A single shared controller instance handles a particular request typecontrollers, interceptors run in the IoC container
Allows multiple DispatcherServlets that can share an “application context”
Interface based not class-based
Posted in: