0% found this document useful (0 votes)
480 views16 pages

SB Ashok It

Spring is a framework for building lightweight, loosely coupled Java applications. It provides APIs for database access, REST services, security and more to help develop distributed applications faster. Dependency injection links dependent objects by injecting their dependencies. The Spring container manages object lifecycles and handles dependency injection through configuration files or annotations.

Uploaded by

subhabirajdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
480 views16 pages

SB Ashok It

Spring is a framework for building lightweight, loosely coupled Java applications. It provides APIs for database access, REST services, security and more to help develop distributed applications faster. Dependency injection links dependent objects by injecting their dependencies. The Spring container manages object lifecycles and handles dependency injection through configuration files or annotations.

Uploaded by

subhabirajdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Q .1 what is spring?

 It is a framework which is used to making light-weight, loosely coupled


enterprises application in java.
 It provides pre-defined API for database operations, Restful web services, micro
services and security.
 We can develop distributed applications very faster. It is mainly for backend
application development using java.

Q .2 what are the features of Spring Framework?


 Spring provides
 Predefined template
 Loose coupling
 Light weight
 Easy to test
 Powerful abstraction
 Fast development(RAD)

Q. 3 what is dependency injection (DI)?


 Linking the objects are injecting objects to dependent class.
 Consider we have A class & B class, A class need B object then B object should be
given to A class.
 Creating B object & giving to A is nothing but injecting the dependency.
 Dependency injection mechanism mainly to create the object in order and inject
one to another in the automation process.
 For this we need to write configuration write in xml format or also we use
configuration @Autowired.

Q. 4 what are the types of dependency injection (DI)?


 Constructor Injection: here object is created by using parameterized constructor
and values are injected.
 Setter Injection : the default constructor is used and setter method is called)
 Field Injection : like @Autowired
 Circular Injection: A is depending B object and B is depending on A object
 Lookup method Injection : it is occur scope mechanism if 2 objects are connected
, first class actually scope is singleton and second class is prototype scope then
we are go this mechanism
 But most we use constructor injection and Field injection

Q. 5 what are the types of dependency injection (DI)?


 Spring container uses Dependency injection mechanism implemented means
object creation, managing and linking, life cycle implementation everything
take care by Spring container.
 This IOC contains tasks to provide input either by xml configuration
annotation, define classes, we need to provide as input.

Q. 6 Spring Configuration File ?


 if I want to tell container to create the object we need to give information. Like
What us class name, what is object name, how many classes are there, how many
objects are require everything we write in xml file and in the
application.properties file using actuator. That xml file provide entire information
and links about that. i.e actually configuration file
 It is also called xml configuration file.
<bean id=”obj1” class=”com.app.EmpService”>
<property name=”empType”>
<value>expert</value>
</property>
</bean>

Q. 7 Spring Bean ?
 It is an object
 This object is completed created, linked with another object and destroy by using
container itself.
 An object the entire process control by spring IOC I actually spring bean.
 This object are take care configuration file with the help of configuration
file(Spring container )

Q. 8 Constructor injection and Setter injection ?


 If a class has four variable and constructor for those variable then at the time of
creating object, we need to pass entire variables.
 But setter injection is not like that set only two variable only and u can ignore
that is called partial injection.
 Constructor injection you must mention all properties or at list null values that is
called no partial injection. Constructor injection generally creates always new
object ,
 but setter injection we can modify. Setter injection never create new object
existed one modify.
 If u have multiple properties and you pass all in order the prefer is constructor
injection.
 Now, I want only five and later on go for remaining few injection then
recommended for setter injection.
 Constructor injections done then we can modify data using setter injection but
we can’t use constructor injection continuously existed modify values so
constructor, one for object creation.

Q. 9 What are the Bean scope is spring provided ?


 General meaning of scope indicate how long an object is present in the container.
 Mainly there five types of scope in spring
 1) Singleton : this scope says that the container here is going to create one
object, for our configuration(any type of application)
 2)prototype:every time new object is created and it accessed that .
 3)request 4)session : request and session are related to web application only.
Request indicate, if browser makes a requestor client application makes any
requests object is created in spring container.
 Once the response is committed the object is destroyed in container.
 Session is nothing but http session like login, logout.
 When you login http session is created then container create its related object
and logout session invalidate and destroy container destroy the object.
 Global-session (not existed in new version of the spring)

Q. 10 What are the Bean scope is spring provided ?


 Global-session (not existed in new version of the spring)
 Linking (combining) two objects
 as I mention class A & class B objects I want to link based on relation that linking.
 One is dependent object. second is dependency object(injecting dependency in
dependent class of bean wiring)
 Example: here empService and emprepository (empRepository object I want to
create and inject to empService that is called wiring.

Q. 11 What is Autowiring in spring ? What are the different models it have ?


 Whatever wiring process we discussed previously that should be automated.
 Without writing reference tags or anything so container will detect for the class,
what are the required classes & their objects based on the relation that is called
association mapping or hazard relation .
 that will choose or pick all the objects and link them that is called Auto wiring .
 in case of xml, there is
1. No
2. byname
3. byType
4. constructor
5. autodetect ( removed in latest version)
 No, indicates it says container hey you don’t do any auto wiring , I’ll do manual
wiring.
 Byname and byType comes under setter injection.
 constructor come under constructor injection internally they used.

Q. 12 What is difference between @Autowiring and @Inject ?


 both work same but
 @Autowired given in spring framework.
 @Inject given by java double e framework.
 Both are used for dependency injection spring support both but
 In case of non-String application objects are java double e application are there
then @Inject was used.
 There was a concept called java CDI(context and dependency injection) is a
mechanism is there in java double E6.
 But nowadays it is not used to much, now currently we use @Autowired but still
you can use @Inject also, the point is inject is not from spring framework,it is
from pure java.

@Autowired @Inject
1) It is use for dependency injection. 1) it is use for dependency injection
2)@Autowired given in spring 2) @Inject is given by java double e
framework. framework.
3) In case of String application objects 3) In case of non-String application
are spring application. then we use objects are java double e application
@Autowired. are then @Inject we use.
4) Mostly used this nowadays. 4) CDI concept was use I java double e.
5) Here @Autowired done by Spring 5) Here inject is not from spring
framework. framework it is from pure java.

Q. 13 What is difference between @Component and @Bean?


 Both indicates for object creation but
 We have source code, we can open the class and top of the class we can write
@Component
 If a class is programmer define means we created class then we can use
@Component.
 If a class is predefined, You can’t modify existed code, if you feel then we use java
based configuration by writing @configuration @bean then container will be
create object.
Q. 14 How many types of IOC containers are there in spring ?
 Two types of containers.
I. BeanFactory :
it takes care of bean creation, destroy bean. This is going to take only xml
configure. It is like a factory class that contains collection of beans. It
instantiate the bean whenever asked for by clients.
II. ApplicationContext :
This interface is built on the top of beanFactory interface. It internally
extends BeanFactory interface but it has other features like : Annotations
support, Multicinticontainer system, Reactive programming. etc

Q. 15 what is the use of @Required annotation in spring framework?


 Generally, This annotation can be written top of the setter methods(@Required is
applied to bean property setter methods)
 Consider a variable have the class and that need set using setter injection, if you
don’t set the value default will be null.
 Then spring container will never ask hey provide the value, if you @Required
container says injecting the values field is mandatory. Must be given. Internally
followed @Autowired followed @Required annotation.

Q. 16 use of @Autowired annotation in spring framework?


 If you want to link two beans means automatically based on the types deduction
beans injection, this annotation is used mostly(It is used to link(inject) two beans
inside spring container).
 Example: If you have service layer, repository layer, controller layer, one layer
you want to link another layer, we can write @Autowired.
 Even we can define @autowired for interface dependency also at runtime it will
choose implementation objects.
 Mostly recommended using field injection.(Actually recommended for field
injection mostly)
Example :: (Here service object is linked with repository object.)
class EmpployeeService{
@Autowired
Private EmployeeRepository repo; }

Q. 17 Use of @Qualifier annotation in spring framework?


 Generally, @Autowired search for implementation object, If there are multiple
objects found then we can choose one object of child type using @Qualifier.
 Example : Consider A class & B class, A class use B object but B is provided three
objects b1, b2, b3 then at the time of writing @Autowired , I can use
@Qualifier(“b2”) means A object I connected to B object which is named with b2.
 Simply, we can say manual selection of child class object. That actually handles
ambiguity mechanism by mentioning manual object.
 [ when we create one or more bean of same type and want to wire only one of
them with a property. We can use the @Qualifier annotation along with
@Autowired to remove the ambiguity by specifying which exact bean should be
wired.]

Q. 18 what is @Primary annotation in spring framework?


 This is also to handle same type of issue i.e done by @Qualifier but this is actually
Qualifier we will write at the time of writing @Autowired but primary will write at
the time of bean creation. If there multiple objects possible available for a child,
then we decide for using primary before Qualifier or no qualifier is used to
highest priority.
 Mostly recommended using field injection.(Actually recommended for field
injection mostly)
 [ indicates that a bean should be given, preference when multiple candidate are
qualified to autowired a single value dependency. In case of autowiring process,
if no qualifier is used @primary is given highest priority ]

Q. 19 what are the life cycle methods in spring framework?


 There are two lifecycle methods
 Init() and destroy() method
 These methods are executed by spring container the order is like creating object,
providing the data using setter injection, calling init() method and you can use
object in the middle, destroy() method will call and finally object will be destroy.
 [ lot of people thing destroy() method destroy the object, it is no, destroy method
is like to do some task before destroying the object(like cleanup process)]

Q.20 What are the different ways of writing life cycle methods in spring ?
 1)There are three ways we can write lifecycle methods using xml(called as
declarative approach)
 By writing bean tag, at bean tah level ,we can pass init-method, that method pass
the method name and destroy happen.
 Example: <bean init-method=”” destroy-method=””></bean>
 2) using spring interfaces(called as programmatic approach)
 There are two interface given implement interface:initializationBean,
Disposablebean. We have to implement this interface and override the methods.
 Using annotation @Poststruct and @PreDestory(This methods are optinal)
Q.21 What are the common implementation of applicationContext?
 applicationContext means spring container. It is an interface.
 The first implemtation, Filesystemxmlapplication context, you can have this xml
file wherever in the project or outside of the project in your system. It can be
loaded, we need.
 Write full path of xml file
 2) classpathxmlApplicationContext :it is also class, it is inside the
project/src/resource folder your xml file must present inside the project. In the
classpath or classpath location where you saved in os.
 3) webxmlApplicationContext :generally we used to implement web based
application using web xml configuration file like servlet other implementation
@configureApplicationcontext and configurableApplicationContext and so on.

Q.22 what are the stereo type annotations?


 Five stereo type annotations.( first you use this type of the class then container
will create the object(bean) to your class. That is actually called as stereo type.
 1) @Component : it is generic one, we can add in any class.
 2) @Service : generally we write business logic.
 3) @Repository : to implement the database operation.
 4) @Controller : to implement http web base application.
 5) @RestController : used for web service and API development.

Q.23 what are different modules in spring?


 Spring is a collection of many modules.
 In we will organized way, those are given to groups, core container, data
access/Integration module(Aspect oriented programming), instrumentation and
test.

Spring framework runtime


Web (mvc /remoting)
Data access Intergration
Web, Servlet, portlet
JDBC,
ORM,OXM,JMS,transaction

AOP instrumentation

Bean Core Core container Expression language


s
Test
Q.24 what are different modules in spring?
 Will be having multiple Environments like development stage, Testing, UAT,
production.
 If you move your application one environment to another environment then our
properties will be modified like will be having database connection mailing
properties.
 Those instead of modifying every time, we can activate mentioning a profile by
using key at runtime to ( we can activate the current profile using)
 --spring.profiles.activate=qa(by passing at runtime)
 Also we can use vm argument
 We can maintain multiple properties files to execute based on the active profile.

Q.25 what is inner beans in spring?


 The name indicates defining one bean inside another bean, it’s not reusable but
so we write another bean tag, property tag, we are constructor in bran tag, we
can implement by using setter injection property tag constructor injection using
constructor are tag.
 First we create outer bean then property in that use constructor then again bean
tag that is inner class bean then we define configuration and close tag
 Example :
<bean id=”outer-bean-tag” class=”outerBean classname”>
<property name=”inner-bean-obj”>
<bean class=”innerBeanClass Name”/></property></bean>
 The benefit of inner bean actually fast in execution, when parent is loaded child
also loaded, when parent is unloaded from memory , child also unload.

Q.26 what is spring WEB Module?


 This module I given by spring framework
 If you want to implement mvc based application. Web application and rest
applications, we are going to this module.
 This module is completely implemented using http protocol standard.
 Like get, post, put, delete, response, status code , it’s internally use.
 By using this web module we are going to implement application(like browser-
server, server-server application means web-services
 Example : bookmyshow interacting with googlepay.
Q.27 MVC?
 It is software architecture design pattern.
 Model :view-controller
 It is separating three things model, view, controller, so developing the application
parallel.

Q.28 spring MVC?


 MVC given by java framework.
 It is used to implement dynamic web application.
 It follows model-view-controller design pattern
 Model: model indicates, it can single of or collection of object, it contains data of
the application, it is actually shared between view and controller.
 Controller : business logic of the application, we can call request processing also
 View : a final representation that is given to end customer are client machine,
here view is java base view like jsp, jstl, apache velocity, thymeleaf, freemarker.

Q.29 what is front container of spring MVC?


 Each and every request taken by a pre-defined servlet in spring mvc that is
dispatcher servlet. It Is pre-defined class exist in the package
org.springframework.web.servlet.
 This one we no need to define, but it actually takes care of all the request. It will
redirect to appropriate controller as a programmer, we need to configure
dispatcher servlet in web.xml file
 Servlet tag, servletmapping tag

Q.30 Explain flow of spring MVC?


 Every request by dispatcher servlet a a front controller.
 This one take help of handler mapping to identify exact controller and it’s
method from the xml configuration.
 One constructor executed, it return some output along with view details, called
as Model and view.
 Finally with the help of view resolver it’s view data and reads into model
representation rended in html format given back to client.

Q.31 Advantages of spring MVC framework?


 1)Light weight : compare to any other mvc framework, here single servlet
mechanism and it’s comes with any UI representation we can add dynamically
single classes.
 Rapid development: concrete UI and controller, if UI change not affect that
much, we can implement them parallel.
 Flexible mapping: we have lot of annotations and easy to redirect like
@getpropertty, @RequestBody, we can implement application very simple
manner.

Q.32 which annotations are used for http request methods?


 Generally, request coming from any client then http request, we have to map
with one different types of methods
 Likes @Getmapping @Postmapping @putmapping @deletemapping
@patchmapping
 @GetMapping: presenting fetching the data from server to client
 @Post Mapping: indicates creating a new resource to the at server
 @Putmapping indicates modified excised data server.
 @Patch mapping indicates partially modifying the data
 @Delete Mapping indicates removing data at server.
 If u are working with web-mvc, we use get & post
 If u are working with web- Rest
 use @getmapping --... ||-- @deletemapping (we use all.)

Q.33 @Controller and RestController


 @RestController = @Response Body + @Controller
 so here @ Response Body we need not to handle manually in case of
@RestController.
 In @Controller convert the body into manually here @controller generally
returns view resolver base output. finally it will go html representation

Q.34 @RequestBody and @ResponseBody


 If you are working with spring Rest based application mostly this two are used.
 incoming request, is actually contains Json data or xml data you want to convert
into object format then @RequestBody use. It is called de-serializing the data
converting JSON to object format, Internally uses http request for converter.
 (Spring automatically de-serialize incoming http request to java object using
http message converter. )
 @Response Body, works on method return type, so @RequestBody annotation
indicates (@Response body annotation indicates that we’ll write the return type
of the method directly to the http response body.
Q.35 How to map controller class and it’s methods with url ?
 we can use a Request Mapping at base annotation class level & method level.
 other http methods(get, post, put delete patch these we can apply only at
method But here I want map url to controller level and method level.
 So we have to specify url and methods type all the details.
 Example:
@Controller
@Requestmapping(“/form” //class level
Class StudentController{
@Requestmapping(“/show”){ //method level
Public string display(){ }

Q.36 How to map controller class and it’s methods with url ?
 When we extract the data incoming request URL that is send by / symbol
representation, you want to read that by using method handler parameter

Q.37 @ResponseBody in spring MVC ?


 Generally, mvc application returns http format but you want to represent JSON or
xml, add @ResponseBody top of the method.
 [The Response Body annotation is used to serialize the returned object
automatically is JSON & bind. it with the http response body.
Q.38 what do you understand by validations in spring MVC ?
 If end your make request that request contains Some Input, that inputs, i want
validate, I want Restrict then we can use @valid
 If u want to use this validation concept spring version must 4 or higher & you
must add Bean validation API in pom.xml or build gradle file.
 Some validation like: @NotNull @Pattern @email @post: used for paste concept
future and post
 @NotBlank : not null, not empty are allowed we have to use @vald annotation
along with @requestBody or @ModelAttrribute.

Q.39 which annotations are used to define Global Exception Handler class?
 Here we use @ Controller Advice or@Rest ControllerAdvice along with
@Exceptionhandler.
 Generally we can use try-catch block but in case of repeated try-catch block are
there. i want to define all for common exception in one place, so I have go for
Generic exception handling code we can we .
 If u are using web mvc the @ControllerAdvice
 If u are using webRest @RestControllerAdvice.
 we need to define one user class with annotation @ControllerAdvice or
@RestControllerAdvice
 Then we need to define one method at top of method @ExceptionHandler in
parameter. we can mension exception type like usernotFoundException,
insuffiecientBalanceException, parameterTypeException, exception .

Q.40 Explain some Response status codes?


 200 indicates OK success
 400 Bad Request (input data is invalid)
 401- UnAuthorized (Login Request is failed)
 403 Forbidden (User not allowed to access Resourse )
 404 Not Found (URL is not exist)
 405- Method Not Allowed (Request method is invalid) you made get Request but
program in post.
 600 Internal Server error
 201-Created

Q.41 what is cross-origin concept in string WEB?


 Cross-origin is security mechanism, it is called as CORS (Cross-origin Resource
sharing) .
 If you are request is coming from web browser by using javascript application.
if it’s wants to producing or consuming where going to say ok it it's not allowed
default.
 where If you are working with integration application like angular or Reacts, then
in spring web application- side we have to add one annotation like
@CrossOrigin ( origins = “http://localhost:8080”)
 If a mension this then the request is allowed else not allowed (It says, cors
blocked

Q.42 what is init binder in string WEB?


 Request is dispatch to the controller before Controller, I want to do some
preprocessing are converting some logoc for each request then
 @InitBinder is used. Webdata initBinder also called. Generally application send
date & Text presentation string. Then I want to convert date class then we can
use @initBinder.
 Even you are getting string (I want your user type of class, by taking 2,5
parameter then also we can use.
 [@initBinder- This annotation is used with the method which initializes
WebDateBinder and works as a preprocessor for each request coming to the
Controller
 like string to Date Format Conversions Even string to any special classType
Conversions.

Q.43 @PathVariable and @Requestparam?


 Both can be used in web MVC Rest,
 The difference is sending the data.
 1) @Pathvariable :If you are sending the data by using @PathVariable then data
can be send along with Request url by using / symbol directly data found follows
the Order
 [ we bind the request URL template path variable to the method variable. -
Ex:http: http://localhost:8080/hello/100/Raghu the 100 value is bound to the id
variable and the Raghu value to the name variable. ]
 There is no key value must be follow.
 2) @RequestParam :, It is also called query string or Query Parameter, start from
question mark
 symbol, key = value, we can send in any order.
 it actually Read by @RequestParam, @RequestParam extract the data from
query string.
 @Parhivariable extract values from URI Path.
 [ with @RequestParam annotation, we com extract valves from the query string.
for ex: http://localhost:8080/hellower?? id=100 and name = Raghu.
 while @Requestparam extract values from the query string.
 @Pathivariable extract values from the URI path. ]

Q.44 why do we need resolver in spring mvc?


 view Resolver purpose is find the output
 [ it is Responsible for rendering of models in the web browser ]
 In Simple create dynamic html page.
 The "InternalResourceViewResolver" is mostly used view resolver in spring MVC.
in spring mvc.
 Some important view resolver’s in spring mvc
 1) AbstractCashingViewResolver
 2) XmlViewResolver
 3) UrlBasedViewResolver.. etc

Q.45 what is the use of @ModelAttribute?


 It binds data, is actually coming from the client you want to bind the method
parameter in return type also. you can use @ModelAttribute.
 [ It binds a method parameter or a method return value to a named model
attribute then exposes it to a web view.]
 In html form, registration form, inquiry form, Login form, payment form that I
want to convert as object (like employee abject) then we can we
@model Attribute & you can Read that object into method parameter.
 [ If a html Form is used and submitted with data then it is converted into one
class object ( like Employee object) & that can be read into code using
@ModelAttribute.
 Syntax; @ModelAttribute("ObjName") className localvariable.

Q.46 is @Controller and @RestController are strereotype?


 Yes, both @Controller & @RestController are stereotypes.
 The @Controller is subtype. of @Component, are specialization of @Componect
 @component is parent, &@ Controller is child.
 Where @RestController is subtype of @Controller hierarchy comes like
@component -> @Controller -> @RestController.
 @RestController internally contains Response but not only that. It even indicates
Restful applications.

 why can't use @ Restcontroller with Responsebody, Yes we can use and we can
get output, but standard is @RestController you have to use that If you are
implementing Restfulweb services and Request process.
Q.47 what is the main difference between Spring Care and Spring MVC?
 spring care is basic concept, that is handle object mechanism and all.
 object mechanism and all.
 where spring mvc build top of spring core
 spring core is actually called as IOC container to (dependency injection) in various
bean classes.
 where spring mvc internally uses IOC mechanism work for web application
implement
 spring container concepts like: container system, Beans, scheduling, AOP etc. are
part of spring core.

Q.48 what are the ways of reading data from the form in spring mvc?
 If your request form is submited then we use legacy technique
HttpServletRequest as a method parameter, and we can use
Request.getparameter but the default always return sting representation, you
have to do manual conversion.
 [ HttpServletRequest interface: The HttpServletRequest is a Java interface
present in Java.servlet.http package]
 @RequestParam that is also read the data even it is support type conversion.
 [@RequestParam: The @RequestParam reads the data binds it automatically to
the parameter. ]
 we can use, @ModelAttribute, entire form in to one object, all are method
parameter only here.
 [ @Model Attribute: The @ModelAttribute binds a method parameter.]

Q.49 what is ResponseEntity<T> in spring Rest?


 Response Entity <T> Representation or Response Entity <?> It is generic,
 If you are using RestController final output is Response Entity.
 It must be used for method Return type
 Generally, ResponseEntity contains Body( i.e Response Body), Http Headers &
Response status code.
 If you are returning class type non string representation employee, List< Details>
that will be converted into xml or JSON format Global formats by using
@ResponseBody Convert.
 [It can be converted into json /xml Formats using @ResponseBody, for non-string
return types. ]

Q.50 why @ RequestHeader is used in Spring WEB?


 If a request is made, Specially for body & Header content, Header Contain ,Header parameter
from key & value. which are sent by client.
 [ This annotation is used to read Header Parameters sent by client application ]
 I want to read only Header parameter like Authorization Header, content type Head request
format Header even some custom headers too, to pass my own key value then we can use
@Request Header Annotation,
 Example: @Request Header ("Authorization") string authbey

You might also like