Spring

The fact that you have @Transactional on a method in a @Service annotated class along with a TransactionManager means the whole transaction lifecycle will be managed by Spring.
When your saveEmployee method is called, Spring will open a Session, start a transaction, execute your code, commit the transaction and close the Session. The Session it starts is bound to the current thread and available through getCurrentSession().
If you instead use openSession(), you are opening a completely unrelated Session, not managed by Spring's TransactionManager. As such, the transaction won't be committed and the Sessionwon't be closed unless you do it yourself.



Modules in Spring:

The core container: The core container provides the essential functionality of the Spring framework. A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory applies the Inversion of Control (IOC) pattern to separate an application's configuration and dependency specification from the actual application code.

Spring context: The Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internalization, validation, and scheduling functionality.

Spring AOP: The Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework, through its configuration management feature. As a result you can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.

Spring DAO: The Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAO's JDBC-oriented exceptions comply to its generic DAO exception hierarchy.

Spring ORM: The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply to Spring's generic transaction and DAO exception hierarchies.

Spring Web module: The Web context module builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.

Spring MVC framework: The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, etc


Design Patterns used by Spring framework:

Spring uses different design patterns, the most important ones are:

- Proxy pattern, Used by AOP, and remoting.
- Singleton, the beans defined in spring config files are singletons by default.
- Template method, to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate, HibernateTemplate
- MVC, used by Spring MVC, advantage is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing   to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it   easier to reuse controllers for different view technologies.
- Front Controller, used by DispatcherServlet to ensure an incoming request gets dispatched to your controllers.
- View Helper, Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views  

Configuring Spring - Application Context 

- ApplicationContexts can be created declaratively using for example a ContextLoader.
- Of course you can also create ApplicationContexts programmatically using one of the ApplicationContext implementations.
- First, let's examine the ContextLoader and its implementations.
- The ContextLoader has two implementations: the ContextLoaderListener and the ContextLoaderServlet.

They both have the same functionality but differ in that the listener cannot be used in Servlet 2.2 compatible containers. Since the Servlet 2.4 specification, listeners are required to initialize after startup of a web application. A lot of 2.3 compatible containers already implement this feature. It is up to you as to which one you use, but all things being equal you should probably prefer ContextLoaderListener; for more information on compatibility, have a look at the JavaDoc for theContextLoaderServlet.

- You can register an ApplicationContext using the ContextLoaderListener as follows:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>


<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
-->
- The listener inspects the contextConfigLocation parameter. If it doesn't exist, it'll use /WEB-INF/applicationContext.xml as a default.
- When it does exist, it'll separate the String using predefined delimiters (comma, semi-colon and space) and use the values as locations where application contexts will be searched for.
- The ContextLoaderServlet can - as said - be used instead of the ContextLoaderListener.
- The servlet will use the contextConfigLocation parameter just as the listener does.

Spring Bean Life Cycle: 

Instantiate – First Spring instantiate the bean.
Populate properties- Spring Inject the bean’s properties.
Set Bean Name- Spring set bean name. if the bean implements BeanNameAware, spring passes .The  bean’s id to setBeanName() method.
Set Bean factory-If Bean implements BeanFactoryAware ,spring passes the beanfactory to          setBeanFactory().
Pre Initialization-  It also called postprocess of bean . if there are any bean BeanPostProcessors,  Spring calls postProcesserBeforeInitialization () method.
Initialize beans-   If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is  Called.
Post Initialization-  If there is BeanPostProcessors, is implements , spring calls their postProcessAfterinitalization() method.
Ready to use- Now the bean is ready to use by the application.
Destroy- If the bean implement DisposableBean , it will call the destroy() method . If custom  destroy () method is defined . the specified method is called.

 Bean scopes:


- singleton Scopes a single bean definition to a single object instance per Spring IoC container.
- prototype Scopes a single bean definition to any number of object instances.
- request Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every   HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
- session Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
- global session Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid  in the context of a web-aware Spring ApplicationContext.