1) What Is Spring?
1) What Is Spring?
1) What Is Spring?
Container − Spring contains and manages the life cycle and configuration of
application objects.
Core module
Bean module
Context module
Expression Language module
JDBC module
ORM module
OXM module
Java Messaging Service(JMS) module
Transaction module
Web module
Web-Servlet module
Web-Struts module
Web-Portlet module
The best solution is using constructor arguments for mandatory dependencies and
setters for optional dependencies. Note that the use of a @Required annotation on a
setter can be used to make setters required dependencies.
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 <bean/> 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.
The bean definition contains the information called configuration metadata which is
needed for the container to know the followings −
When defining a <bean> in Spring, we can also declare a scope for the bean. It can
be defined through the scope attribute in the bean definition. For example, when
Spring has to produce a new bean instance each time one is needed, the bean’s
scope attribute to be prototype. On the other hand, when the same instance of a
bean must be returned by Spring every time it is needed, the the bean scope
attribute must be set to singleton.
singleton: Only one instance of the bean will be created for each container.
This is the default scope for the spring beans. While using this scope, make
sure spring bean doesn’t have shared instance variables otherwise it might
lead to data inconsistency issues because it’s not thread-safe.
prototype: A new instance will be created every time the bean is requested.
request: This is same as prototype scope, however it’s meant to be used for
web applications. A new instance of the bean will be created for each HTTP
request.
session: A new bean will be created for each HTTP session by the container.
global-session: This is used to create global session beans for Portlet
applications.
Instantiate: First the spring container finds the bean’s definition from
the XML file and instantiates the bean.
Populate properties: Using the dependency injection, spring populates
all of the properties as specified in the bean definition.
Set Bean Name: If the bean implements BeanNameAware interface,
spring passes the bean’s id to setBeanName() method.
Set Bean factory: If Bean implements BeanFactoryAware interface,
spring passes the beanfactory to setBeanFactory() method.
Pre Initialization: Also called post process of bean. If there are any
bean BeanPostProcessors associated with the bean, 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 are any BeanPostProcessors associated
with the bean, their postProcessAfterInitialization() methods will be called.
Ready to use: Now the bean is ready to use by the application
Destroy: If the bean implements DisposableBean , it will call the
destroy() method
<list> − This helps in wiring i.e. injecting a list of values, allowing duplicates.
<set> − This helps in wiring a set of values but without any duplicates.
2) byName injects the bean based on the property name. It uses setter
method.
3) byType injects the bean based on the property type. It uses setter method.
21) Can you inject null and empty string values in Spring?
Yes.
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.
A module which has a set of APIs providing cross-cutting requirements. For example,
a logging module would be called AOP aspect for logging. An application can have
any number of aspects depending on the requirement. In Spring AOP, aspects are
implemented using regular classes (the schema-based approach) or regular classes
annotated with the @Aspect annotation (@AspectJ style).
This represents a point in your application where you can plug-in AOP aspect. You
can also say, it is the actual place in the application where an action will be taken
using Spring AOP framework.
JoinPoint is any point in your program such as field access, method execution,
exception handling etc.
This is the actual action to be taken either before or after the method execution. This
is actual piece of code that is invoked during program execution by Spring AOP
framework.
after − Run advice after the a method execution regardless of its outcome.
around − Run advice before and after the advised method is invoked.
This is a set of one or more joinpoints where an advice should be executed. You can
specify pointcuts using expressions or patterns as we will see in our AOP examples.
The object being advised by one or more aspects, this object will always be a proxy
object. Also referred to as the advised object.
Weaving is a process of linking aspect with other application. Weaving can be done
at compile time, load time, or at runtime. Spring framework performs weaving at
runtime.
1. Spring AOP
2. Apache AspectJ
3. JBoss AOP
The @Controller annotation marks the class as controller class. It is applied on the
class. Spring does not require you to extend any controller base class or reference
the Servlet API.
The @RequestMapping annotation maps the request with the method. It is applied
on the method. @RequestMapping annotation is used to map a URL to either an
entire class or a particular handler method.
The View Resolver class resolves the view component to be invoked for the request.
It defines prefix and suffix properties to resolve the view component.
The org.springframework.web.servlet.view.InternalResourceViewResolver
class is widely used.
48) Does spring MVC provide validation support?
Yes.
Less code: By using the JdbcTemplate class, you don't need to create
connection,statement,start transaction,commit transaction and close connection to
execute different queries. You can execute the query directly.
You can fetch records from the database by the query method of JdbcTemplate.
There are two interfaces to do this:
ResultSetExtractor
RowMapper