SPRINGS

1.what is spring?

ans:

Spring Framework is a powerful lightweight application development framework used for Enterprise Java (JEE).

The core features of the Spring Framework can be used in developing any Java application. It can be described as complete and modular framework. 
The Spring Framework can be used for all layer implementations of a real time application. It can also be used for the development of particular layer of a real time application unlike Struts and Hibernate, but with Spring we can develop all layers.

2.what are the different modules of spring?

ans:

Spring has the following modules:

The Core container module – The fundamental functionality is provided by this module

Application context module - Spring is a framework because of this module. This module extends the concept of BeanFactory.

AOP module (Aspect Oriented Programming) – This is module is for using aspects of spring based applications.

JDBC abstraction and DAO module – To make database code cleanup and to prevent problems at the time of closing database resources, this module is used.

O/R mapping integration module (Object/Relational) – This module is used to tie up with ORM tools such as hibernate.

Web module – The context for an appropriate web-based application, this module is used.

3.what is IOC?

ans:

In Spring, IOC is a Container which has 2 containers i.e Core Container and J2EE Container . Core Container is also called Spring BeanFactory Container and J2EE container is also called Spring ApplicationContext Container.

The Containers are responsible for the following functions in a application.

Read the XML.
Create the instances of Pojo Classes.
Manage the life-cycle of Pojo Classes.
Dependency Injection.

The IOC container reads the configuration metadata from the XML provided by the user. IOC uses the SAX parser to read the XML file.
Sax parser validates the xml by looking over the opening and closing tags and checking whether the tag is valid or not. It maintains the well-formed of the XML File.

Dependency Injection Comes in picture when we want the application to be developed in the loose-coupled environment.
What we do here is, we try to not Hard-Code (e.g use of new keyword ) the services required in the POJO classes.

4.What is loose-coupling?

ans:

In loose-coupling, what we try is to design the code in such manner that it must not be dependent on the other class object. If in future we don’t need that object or we want to change it, then it must not be dependent on that particular object.we achieve the loose-coupling by the concept of abstraction and dependency injection .

The dependency/ service can be anything, it maybe a object, connection pool, JDBC template .

So, IOC provides the feature of the Dependency Injection where it reads the data from the xml file in run-time and pass it to the POJO class. 
Dependency Injection Container are responsible to create a object for you and inject it in your class.

5.What is dependency Injection?

ans:

Dependency Injection(DI) means to decouple the objects which are dependent on each other. Say object A is dependent on Object B so the idea is to decouple these object from each other. We don’t need to hard code the object using new keyword rather sharing dependencies to objects at runtime in spite of compile time.
 
How Dependency Injection works in Spring:

We don’t need to hard code the object using new keyword rather define the bean dependency in the configuration file. 
The spring container will be responsible for hooking up all.

Inversion of Control (IOC)

IOC is a general concept and it can be expressed in many different ways and Dependency Injection is one concrete example of IOC.

Two types of Dependency Injection:

Constructor Injection
Setter Injection

1. Constructor-based dependency injection:

Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

public class Triangle {
private String type;

public String getType(){
    return type;
 }

public Triangle(String type){   //constructor injection
    this.type=type;
 }
}

<bean id=triangle" class ="com.test.dependencyInjection.Triangle">
        <constructor-arg value="20"/>
  </bean>

2. Setter-based dependency injection:

Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument
static factory method to instantiate your bean.

public class Triangle{
 private String type;

 public String getType(){
    return type;
  }
 public void setType(String type){          //setter injection
    this.type = type;
  }
 }

<!-- setter injection -->
 <bean id="triangle" class="com.test.dependencyInjection.Triangle">
        <property name="type" value="equivialteral"/>

NOTE: It is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies. 


6.what is the difference between BeanFactory and ApplicationContext?

ans:

Difference between BeanFactory and ApplicationContext are following:

BeanFactory uses lazy initialization but ApplicationContext uses eager initialization. In case of BeanFactory, bean is created when you call getBeans() method, but bean is created upfront in case of ApplicationContext when the ApplicationContext object is created.
BeanFactory explicitly provide a resource object using syntax but ApplicationContext creates and manages resource objects on its own.
BeanFactory doesnt support internatiolization but ApplicationContext supports internationalization.
With BeanFactory annotation based dependency injection is not supported but annotation based dependency injection is supported in ApplicationContext.

Using BeanFactory:

BeanFactory beanfactory = new XMLBeanFactory(new FileSystemResource("spring.xml"));
Triangle triangle =(Triangle)beanFactory.getBean("triangle");

Using ApplicationContext:

ApplicationContext context = new ClassPathXMLApplicationContext("spring.xml")
Triangle triangle =(Triangle)context.getBean("triangle");



7. What is a Spring configuration file?

ans:

A Spring configuration file is an XML file. This file mainly contains the classes information. It describes how those classes are configured as well as introduced to each other. If it’s not planned and written correctly, it becomes very difficult to manage in big projects.

8.How configuration metadata is provided to the Spring container?

ans:

Configuration metadata can be provided to Spring container in following ways:

XML-Based configuration: In Spring Framework, the dependencies and the services needed by beans are specified in configuration files which are in XML format. 
These configuration files usually contain a lot of bean definitions and application specific configuration options. 
They generally start with a bean tag. For example:

<bean id="studentbean" class="com.xyz.StudentBean">
 <property name="name" value="abc"></property>
</bean>

Annotation-Based configuration: Instead of using XML to describe a bean wiring, you can configure the bean into the component class itself by using annotations
on the relevant class, method, or field declaration. By default, annotation wiring is not turned on in the Spring container.
So, you need to enable it in your Spring configuration file before using it. For example:

<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>

Java-based configuration: The key features in Spring Framework’s new Java-configuration support are @Configuration annotated classes and @Bean annotated methods.
 
1.@Bean annotation plays the same role as the <bean/> element. 
2.@Configuration classes allows to define inter-bean dependencies by simply calling other @Bean methods in the same class. 

For example:

@Configuration
public class StudentConfig 
@Bean
public StudentBean myStudent() 
{ return new StudentBean(); }
}



9.what is auto wiring and explain its types?

ans:

Autowiring helps you to resolve dependency injection automatically. You need not give any hints to the IOC container for searching bean instance.
The Spring container can autowire relationships between collaborating beans without using <constructor-arg> and <property> elements which helps cut down on the amount of XML configuration you write for a big Spring based application.

no: This is default setting which means no autowiring. Explicit bean reference should be used for wiring.

byName: It injects the object dependency according to name of the bean. It matches and wires its properties with the beans defined by the same names 
in the XML file.

byType: It injects the object dependency according to type. It matches and wires a property if its type matches with exactly one of the beans name in XML file.

constructor: It injects the dependency by calling the constructor of the class. It has a large number of parameters.

autodetect: First the container tries to wire using autowire by constructor, if it can’t then it tries to autowire by byType.

9.explain bean scopes?

ans:

below are the bean scopes we have in spring.

Singleton: This provides scope for the bean definition to single instance per Spring IoC container.

Prototype: This provides scope for a single bean definition to have any number of object instances.

Request: This provides scope for a bean definition to an HTTP-request. 

Session: This provides scope for a bean definition to an HTTP-session. 

Global-session: This provides scope for a bean definition to an Global HTTP-session.

note: singleton is default bean scope in spring container

10.explain about Spring DAO module?

ans:

The Data Access Object (DAO) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. 
This allows one to switch between the persistence technologies easily. It also allows you to code without worrying about catching exceptions that are specific to each of these technology.

11.what are the important classes of Spring DAO module?

ans:

Below are the important classes.

JdbcTemplate
SimpleJdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcInsert
SimpleJdbcCall

12.what are the transaction management types in spring?

ans:

there are two transaction management types in spring.

Programmatic transaction management: In this, the transaction is managed with the help of programming. It provides you extreme flexibility,but it is very difficult to maintain.

Declarative transaction management: In this, the transaction management is separated from the business code. Only annotations or XML based configurations are used to manage the transactions.


13.Explain about AOP?

ans:

Aspect-oriented programming or AOP is a programming technique which allows programmers to modularize crosscutting concerns or behavior that cuts across the typical divisions of responsibility. Examples of cross-cutting concerns can be logging and transaction management. The core of AOP is an aspect. It encapsulates behaviors that can affect multiple classes into reusable modules.

14.Explain Spring AOP terminologies?

ans:
 
Aspect: The class which implements the JEE application cross-cutting concerns(transaction, logger etc) is known as the aspect. It can be normal class configured through XML configuration or through regular classes annotated with @Aspect.

Weaving: The process of linking Aspects with an Advised Object. It can be done at load time, compile time or at runtime time. Spring AOP does weaving at runtime.

Advice: The job which is meant to be done by an Aspect or it can be defined as the action taken by the Aspect at a particular point. 
There are five types of Advice namely: Before, After, Around, AfterThrowing and AfterReturning.

Before: Runs before the advised method is invoked. It is denoted by @Before annotation.

After: Runs after the advised method completes regardless of the outcome, whether successful or not. It is denoted by @After annotation.

AfterReturning: Runs after the advised method successfully completes ie without any runtime exceptions. It is denoted by @AfterReturning annotation.

Around: This is the strongest advice among all the advice since it wraps around and runs before and after the advised method. This type of advice is used
where we need frequent access to a method or database like- caching. It is denoted by @Around annotation.

AfterThrowing: Runs after the advised method throws a Runtime Exception. It is denoted by @AfterThrowing annotation.

JoinPoints: An application has thousands of opportunities or points to apply Advice. These points are known as join points. For example – Advice can be applied at every invocation of a method or exception be thrown or at various other points. But Spring AOP currently supports only method execution join points
(advising the execution of methods on Spring beans).

Pointcut: Since it is not feasible to apply advice at every point of the code, therefore, the selected join points where advice is finally applied are known 
as the Pointcut. Often you specify these pointcuts using explicit class and method names or through regular expressions that define a matching class and 
method name patterns.

14.what is Spring MVC?

ans:

The Spring Web MVC framework provides Model-View-Controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic),while providing a loose coupling between these elements.

The Model encapsulates the application data and in general they will consist of POJO.

The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

15.Explain spring mvc flow?

ans:

The browser gives request to web application.

As frontcontroller DispatcherServlet traps and takes the request and applies the common system services like security,logging etc.,
DispatcherServlet uses HandlerMapping component to decide the Handler class to utilize based on the incoming request uri.

DispatcherServlet passes the control to Handler class by calling method.
Handler class internally writes the received form data to command class object.

Handler class process the request and generates the output, if needed it also uses command object work with form data.

Sometimes handler class deligates the request to other external components like ejc,webservices and etc.

Handler class returns logical view name back to DispatcherServlet.

DispatcherServlet uses view Resolver to get view object having view layer technology and resource name.

DispatcherServlet uses the view object to pass the control to view 
resource.

The view resource format the results and sends the output to browser(response to browser).

16.Explain about DispatcherServlet?

ans:

The DispatcherServlet follows the Front Controller design pattern. The C in MVC refers to the page controller which retreives the data from the model (your services) and passes it to the view for rendering.

The purpose of the DispatcherServlet is to determine the page controller that is supposed to handle the request and coordinates the model and the view.

Its an advanced servlet which can handle request mapping , locale resolution , view resolution , content negotiation etc.



Comments

Popular posts from this blog