SpringSource Spring Framework tutorials and examples. Focusing on Spring 3 and above.
Following are the benefits offered by the Spring Framework.
It is possible to develop enterprise level application in Spring using POJOS. The major benefit of using POJO is that you can avoid EJB container like application server. In such cases you just need to use a web server like Tomcat or any other commercial product.
A modular style is used to organize Spring. There are lot of packages and classes but you must only consider the classes being used by you.
For Java enterprise application development the most popular development framework is Spring. The Spring framework is being used by the million of developers around the work. It is possible to create easily testable, high performing and reusable code.
The Spring is an open source Java platform based Framewrok. The Spring framework was first released in June 2003 under the Apache 2.0 license and it was written by Rod Johnson initially.
When considering the transparency
The Spring framework has great support for Log4J and it is easy to use Log4J for logging inside Spring applications. In this section, we will discuss the Log4J and Spring integration and how you can use it.
To install the Log4J on you machine, download it from the apache website, extract the zip file and use the log4j.jar in your project.
The Jakarta commons loggin (JCL) API provides an alternative approach to support loggin in your Spring application. Jakarta commons loggin API can
The DispatcherServlet provides the base for Spring web model view controller framework. The DispatcherServlet handles all the HTTP responses and requests. The below diagram explains the request processing workflow based on the Spring Web MVC DispatcherServlet. Spring Servlet Dispatcher
For an incoming HTTP request, following sequence of events are generated.
The HandlerMapping is consulted to call the related Controller when
The model view controller architecture is provided by the Spring Web MVC framework and it also provides ready components which are used to generate loosely coupled and flexible web applications. The different application aspects are separated in result of MVC pattern like business logic, input logic and UI logic. This pattern will provide loose coupling between the elements.
The model data is encapsulated by the Model and it is consist of POJO in general.
The model data
The Spring supports the following two types of transaction management.
Programmatic Transaction Management: In this type of transaction management, you will manage the transaction in your code. This transaction management provide you extreme flexibility and at the other end it is hard to manage and maintain.
Declarative Transaction Management: In this transaction management, the business code is separated from the transaction management. The XML based configuration or annotations
A sequence of actions which make a single work unit is a database transaction. This sequence of actions must be completely taken effect or discarded at all. The most important part of RDBMS based enterprise applications is transaction management which ensure consistency and data integrity. Following are the four main properties of transactions which explain it.
Atomicity: A single unit of operation best describes the transaction. So that is either successful or not as a single unit of operation.
Following functions are performed by JdbcTemplate class.
Execute update statements
Stored procedure calls
Returned parameter extraction
Cache JDBC exceptions
Transalation of JDBC exceptions into generic and informative exceptions that are defined in spring dao package
Once JdbcTemplate instance are configured, they are thread safe. It is possible to use a single JdbcTemplate
It is cumbersome to write useless and unnecessary code when working with the database using old JDBC which is old and plain to handle database connections and exceptions etc. All the minor details are handled by the Spring JDBC framework like open a database connection, prepare a SQL statement, execute a SQL statement, handle transactions, process exception and close a database connection.
You job is to only define the parameters for database connections and SQL statements so that
The Aspect Oriented Programming (AOP) framework is the key Spring framework component. Program logic is breaked down into unique parts by the Aspect Oriented Programming which are called so-called concerns. The cross cutting concerns are the functions of an application which are based on multiple points. The application business logic is seprated from cross cutting concerns conceptually. Following are the examples of aspects:
The ApplicationEvent class and ApplicationListener interface provide the event handling in the ApplicationContext.For each ApplicationEvents that is published to ApplicationContext the bean which has implemented the ApplicationListener is notified.
Following events are provided by the Spring.
ContextRefreshedEvent: At the initializion or refreshing of ApplicationContext, ContextRefreshedEvent is generated. Also the refresh() method is used to generate this event on
The dependency injection can be configured using annotations in Spring 2.5 and above. The configuration of bean class can be done in class itself as compared to XML based bean wiring with the use of annotations on relevant method, class or field declaration.
The XML injection is performed after the annotation based injection so the XML injection will override the annotation injection.
The Spring container will not turn on the annotation wiring by default. So you will
The relationship can be auto wired by the Spring container between collaborating beans. It will reuce the overhead of writing large XML configuration for big Spring based applications.
For dependency injection, following autowiring modes are used by the Spring container.
no: This mode is the default mode and it means that no autowiring and explicit bean referencing should be used for wiring. For this wiring, you do not need to use anything special.
A few working object makes a java application which is seen by the end user as a working application. The application classes must be independent and loosely coupled in case of complex and large Java applications from other Java classes in order to increase reusability chances. Also this approach makes the testing process easy in the unit testing of complex Java application. These classes are integrated together by the dependency injection also known as wiring and it also takes the same amount of
Following information is contained by a bean definition:
Constructor arguments information
Container specific information like method initialization
Property values information
Names of static factory methods
Configuration data is inherited from the parent definition by the child bean definition. Some values are added or overridden by the child definition. There are no direct concerns between the java class
The callback methods are defined by the Bean Post Processor interface and it can be implemented to provide your instatiation logic and dependency resolution logic. When the instantiating, initializating and configuring of a bean is finished by the Spring container, some custom logic implementation can be done by you by integrating Bean Post Processor implementations.
More than one Bean Post Processor interfaces are allowed to use and configure and also it is possible to control the
It is easy to understand the Spring Bean Life Cycle. It is required to perform some initialization on the instantiation of a bean so that it can enter into a usable state. When a bean is not required, some cleanup process is required to remove the bean from the container.
There is a list of activities which are performed behind the scenes during the life of bean instantiation and destruction. Here we will discuss only two important methods of bean life cycle callback. These methods
You can easily declare the bean scope for the bean when you define a bean in Spring. Whenever a new bean instanace is required by you, bean scope attribute is declared as prototype to force the Spring to produce the bean instance. Also whenever you are looking for the same bean instance from the Spring, the bean scope instance is declared as singleton.
The following five scopes are supported by the Spring framework. In case of a web aware ApplicationContext, only three scopes are available.
The beans are the object that forms the backbone of any application and the Spring IoC container manges these beans. The Spring IoC container is responsible for instantiating, assembling and managing of a bean object. The configuration metadata is used to create these beans which are sent to container. For example, they are sent in the form of XML configurations.
The configuration metadata information is contained by the bean definition which is required to know the following by the
The Spring framework core is the Spring container. The Spring IoC container will:
Wire those created objects
Configure thos objects
Manage complete lifecycle of the object
An application is made up of components and these components are managed by the Sping container using dependency injection. These objects are also known as Spring Bean objects.
The configuration metadata is read by the Spring container in
The advanced spring container is the Application Context. Just like the BeanFactory, it can perform the similar operations like wiere beans together, loading of bean definitions and upon request dispense bean. Following enterprise specific functionality is provided:
From properties file, it can resolve textual messages.
Application events are published to event listeners.
The ApplicationContext interface defines this container.
The application configuration is provided and configured by a central interface in spring called ApplicationContext.
Following functionalities are provided by the Spring ApplicationContext.
The ListableBeanFactory inherited Bean factory methods are provided by the ApplicationContext. Also it will help the application to avoid use of singletons in their applications.
The MessageSoruce interface provides the functionality to support internationalization and
The bean has a very simple lifecycle in a normal traditional java application. The bean is instantiated with the new keywork in Java and it is ready to use. When a created bean is not referred by any other object or is not in use, it is collected by the garbage collector.
Bean life cycle in the Spring framework perform extra functionalities. So it is an important part of the Spring framework to understand its bean life cycle so that all the benefits offered by the spring for bean
Factory design implementation is the beans factory. This class has a responsibility is creation or dispension of the beans. Bean factory is basically the general purpose sort of factory. Application objects are instantiated and delivered by the bean factory. Bean factory is aware of various objects present in application. Associations are created b/w the collaborated objects when they are instantiated or configuration burden is being removed from the bean. This also has a vital role in bean life
POJO components are presented by a spring bean which perform certain operations that are considerably useful. All of the SpringBeans which are present in Spring Container are called IOC Container. Spring framework is considered to be very transparent and it hides various infrastructure complexities and communication which occurs b/w spring beans and spring container. Sequence of activities is listed by this section which will be occurring b/w Bean instantiation Bean time and will be handing over
Java classes and components shall not be dependent upon any other java classes. It could be increasing the possibilities of reusage of such classes and they could be tested independently. For decoupling of the java components, there is a need to inject some dependency of various other classes persent.
Class A is dependent upon Class B in case Class B is used as variable.
When dependency injection is being used, in that case Class B would be given to A through:
Spring is considered as a widely deployed and popular deployed open source framework which assists programmers to create higher quality applications quickly. A consistent configuration model and programming is provided by Spring which is understood very well and is used by developers globally. Range of capabilities are provided by Spring to create rich web, enterprise Jave and enterprise applications which could be utilized in a light weighted manner. Spring used for Enterprise
Few associated benefits of Spring that could be brought to some project are listed here: Your mid-tier objects could be organized effectively by Spring whether you use EJB or not. Plumbing is being taken care by Spring which would be there with you when only struts or some other framework is used particular to J2EE APIs. Most probably this is much more valuable for mid tier so configuration management services of spring could be utilized in any kind of architectural layer, no matter
Configuration model and comprehensive programming is provided by the spring framework for the moderanized java based application for any of the deployment platform. Spring key element is the infra-strutural support provided at the level of application: Spring keeps its focus at the enterprise application plumbing therefore teams could keep their focus at the business logic which is application level without any unessential ties with deployment environment.
Spring is one of the most popular application frameworks for Java development. The framework comes with various projects like Spring Core, Spring Data, Spring Web Services, Spring MVC
Each project suits for a particular area, and for the area of web development, Spring MVC is very well-known.
Spring MVC makes web development easier by separating the three components: Model, View, and Controller. The model represents the principal data that is managed by the application.