Spring is an open source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the JavaEE platform.
Spring framework targets to make JavaEE development easier to use and promote good programming practice by enabling a POJO-based programming model.
A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
The most commonly used BeanFactory implementation is the XmlBeanFactory class.
The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring.
Spring also supports for using of an Object/Relational Mapping (ORM) tool over straight JDBC by providing the ORM module.
Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps.
Spring’s transaction management supports each of these ORM frameworks as well as JDBC.
Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
Inversion of Control (IOC) :
Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect Oriented Programming (AOP) :
Spring supports Aspect Oriented Programming and separates application business logic from system services.
Spring contains and manages the life cycle and configuration of application objects.
MVC Framework :
Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.
Transaction Management :
Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
Exception Handling :
Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.
The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.
With the JDBC abstraction and DAO module we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. It provides a layer of meaningful exceptions on top of the error messages given by several database servers.
It also makes use of Spring’s AOP module to provide transaction management services for objects in a Spring application.
You can use both Constructor-based and Setter-based Dependency Injection. The best solution is using constructor arguments for mandatory dependencies and setters for optional dependencies.
The most useful one is
org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.
The basic modules of the Spring framework are :
Expression Language Module
Java Messaging Service (JMS) Module
This is the basic Spring module, which provides the fundamental functionality of the Spring framework.
BeanFactory is the heart of any spring-based application.
Spring framework was built on the top of this module, which makes the Spring container.
The Spring Beans are Java Objects that form the backbone of a Spring application. They are instantiated, assembled, and managed by the Spring IoC container. These beans are created with the configuration metadata that is supplied to the container, for example, in the form of XML definitions.
Beans defined in spring framework are singleton beans. There is an attribute in bean tag named "singleton" if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans.
A Spring Bean definition contains all configuration metadata which is needed for the container to know how to create a bean, its lifecycle details and its dependencies.
There are three important methods to provide configuration metadata to the Spring Container :
XML based configuration file.
Spring offers the following types of collection configuration elements :
The type is used for injecting a list of values, in the case that duplicates are allowed.
The type is used for wiring a set of values but without any duplicates.
The type is used to inject a collection of name-value pairs where name and value can be of any type.
The type can be used to inject a collection of name-value pairs where the name and value are both Strings.
The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for a bean by inspecting the contents of the BeanFactory without using and elements.
The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection :
This is default setting. Explicit bean reference should be used for wiring.
When autowiring byName, the Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.
When autowiring by datatype, the Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exist, a fatal exception is thrown.
This mode is similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.
Wiring, or else bean wiring is the case when beans are combined together within the Spring container. When wiring beans, the Spring container needs to know what beans are needed and how the container should use dependency injection to tie them together.
Limitations of autowiring are :
You can still specify dependencies using and settings which will always override autowiring.
Primitive data types :
You cannot autowire simple properties such as primitives, Strings, and Classes.
Confusing nature :
Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring.
Java based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations.
An example is the @Configuration annotation, that indicates that the class can be used by the Spring IoC container as a source of bean definitions. Another example is the@Bean annotated method that will return an object that should be registered as a bean in the Spring application context.
Annotation wiring is not turned on in the Spring container by default. In order to use annotation based wiring we must enable it in our Spring configuration file by configuring element.
An alternative to XML setups is provided by annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.
Yes, you can.
This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws BeanInitializationException if the affected bean property has not been populated.
The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. It can be used to autowire bean on the setter method just like @Required annotation, on the constructor, on a property or pn methods with arbitrary names and/or multiple arguments.
This is the basic Spring module, which provides the fundamental functionality of the Spring framework. BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.
When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifier annotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired.
JdbcTemplate class provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.
When using the Spring JDBC framework the burden of resource management and error handling is reduced. So developers only need to write the statements and queries to get the data to and from the database.
JDBC can be used more efficiently with the help of a template class provided by Spring framework, which is the JdbcTemplate (example here).