Wednesday 9 September 2015

Spring interview question and answer (J2ee)

Q-what is spring?
ans:- spring is light weight and open source framework ,it is complete modular framwork
i mean spring framework can be used for all layer implementations for a real time application.
Struts[ only for front end related ] and hibernate [ only for database related ], but with spring we can develop all layers 
1>- it provides IOC container, Dependency Injector, MVC flow and many other APIs for the java programme.
 
Q-why spring demand in market?
bcoz of three things
  • Simplicity:--means because as it is non-invasive, POJO and POJI model
  • Testability:-spring has it own container to run the applications.
  • Loose Coupling:--
Q- what is ioc?
IOC (Inversion of Control pattern) is also known as dependency injection 
it will create object of bean class 
it is used to craeted loosely coupled component.
note:- no need to use new keyword to create instance of dependent class only just create reference of dependent class ioc container will set the instance  

note:-Loose coupling is achieved in Spring, with the Inversion of Control technique 

@Write the benefits of using IOC?
 ans-The major benefits of dependency injection or IOC are that it reduces the amount of coding required for the application.
This allows the testing of the application to be done quickly and easily as no JNDI lookup mechanism or singletons are required.
IOC containers also support lazy loading and eager installation of services.
 
  Q-what is spring dao,where to use
Q-what is spring aop,where to use
Q-what is spring ioc,where to use


Q-diff between beanFactory and applicationContext.
ans:-ApplicationContext Container is advanced than Beanfactory Container...
  BeanFactory doesnt support annotation while applicationContext

 
1) BeanFactory Container is basic container, it can only create objects and inject Dependencies.But we can not attach other services like security, transaction ,messaging etc. To provied all the services we have to use ApplicationContext Container.

2) BeanFactory Container doesn't support the feature of AutoScanning , but ApplicationContext Container supports.

3) Beanfactory Container will not create a bean object upto the request time.It means Beanfactory Container loads beans laziely. While ApplicationContext Container creates objects of Singleton bean at the time of loading only.It means there is early loading.

4) Beanfactory Container support only two scopes (singleton & prototype) of the beans. But ApplicationContext Container supports all the beans scope.

Q-What is Auto Wiring?
Autowiring is used to build relationships between the collaborating beans. Spring container can automatically resolve collaborators for beans.
we can say  dependency in the form of object is called autowired.


advantage:-
It requires the less code because we don't need to write the code to inject the dependency explicitly.


Q-What are different types of Autowire?
There are four different types of Auto wire:
  • byName
  • byType
  • constructor
  • autodetect
 
Q-what kind of design pattern in spring
Q-autowiring annotation

Q-what is bean scope available in spring
ans:-
ans-1>singleton scope.
2>prototype
3>session
4>request
5>global session
diffrence between prototype scope and singleton scope ?
If you use scope="prototype" while defining the bean, it will create the new instance always for calling the getBean() method.

You can use scope="singleton", in such case bean instance will be created only once. It is the default bean scope.



request:- Return a single bean instance per HTTP request. 
 session – Return a single bean instance per HTTP session. *
globalSession – Return a single bean instance per global HTTP session. *

request scope="



Q-n which scenario, you will use singleton and prototype scope?

Singleton scope should be used with EJB stateless session bean and prototype scope with EJB stateful session bean.
 
16)diffrence between setter and constructor
17)which one is better beanFacory and applicationContext
18)which one is better setter injection and constructor injection
Q-dependency injection
ans- injecting dependency into dependent that is called dependency
exmaple-if wehave two object one object dependent another object.
1>setter injection
2>constructor injection 

setter injection:-1> In Setter Injection, partial injection of dependencies can possible, means if we have 3 dependencies like int, string, long, then its not necessary to inject all values
example:- Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.

2> if we use setter injection. If you are not inject it will takes default values for those primitives
3>Setter Injection will overrides the constructor injection value 
4>3. If we have more dependencies for example 15 to 20 are there in our bean class then, in this case setter injection is not recommended as we need to write almost 20 setters right, bean length will increase. 
5>. Setter injection makes bean class object as mutable [We can change ]
means : We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection.

 Note:- if we might miss the any property to than it will not give any exception thats why developer more prefer setter injection method 
in case of constructor it will find exact matching

Constructor injection:-   In constructor injection, partial injection of dependencies cannot possible, because for calling constructor we must pass all the arguments right, if not so we may get error.
2:- But, constructor injection cannot overrides the setter injected values
3:-In this case, Constructor injection is highly recommended, as we can inject all the dependencies with in 3 to 4 lines [i mean, by calling one constructor] 
4:-4. Constructor injection makes bean class object as immutable [We cannot change ]  


Q- What is the default scope of bean in Spring framework?
The default scope of bean is Singleton for Spring framework.

Q- Is Singleton beans are thread safe in Spring Framework?
No, singleton beans are not thread-safe in Spring framework.

 Q-What is Auto Wiring?
Autowiring is used to build relationships between the collaborating beans. Spring container can automatically resolve collaborators for beans.

Q- What are the different Modes of Autowiring?
Autowiring has five different modes:
  • no: no autowire
  • byName : Autowiring that can be done by property name
  • byType : property type as autowired
  • constructor: It is similar to byType and it is property is in constructor
  • autodetect :  Spring is allowed to select autowiring from byType or constructor

How to start using spring?Following steps needs to be done to start with the Spring:
  • Download Spring and its dependent file from spring’s site.
  • Create application context xml to define beans and its dependencies
  • Integrate application context xml with web.xml
  • Deploy and Run the application
Q-Differentiate between singleton and prototype bean?
Singleton means only one bean is defined per object instance while Prototype means one definition to more than one object instances in Spring.

Q-What are the types of Dependency Injection?
Two types of dependency injection are supported by spring framework:
  • Setter Injection
  • Constructor Injection

 Q- are the types of the transaction management that is supported by spring?
Following are the types of transaction management that has been supported by spring:
1> programmatically
2>declarative

Q-Describe about DAO in Spring framework?DAO is used to provide integration of Java database connectivity and Object relational mapping objects. DAO is spring framework provides connection for JDBC, hibernate, JDO, JPA, Common client interface and Oracle.
44) What is Autoproxying?
Autoproxying is used to create proxy automatically for the spring users. It provides following two classes to support this automatic proxy creation:
  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator.
Q>What is a Target?
A target is the class that is advised. This class can either be a class to which we want to add a special behavior to or a third party class. The target class is free to center on its major concern using the AOP concepts, regardless of any advice that is being applied.

Q-What does the ViewResolver class?

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.

Q-What does @RequestMapping annotation?

The @RequestMapping annotation maps the request with the method. It is applied on the method.

Q-What are the types of advice in AOP?

There are 5 types of advices in spring AOP.
  1. Before Advice
  2. After Advice
  3. After Returning Advice
  4. Throws Advice
  5. Around Advice
Q-what are the design pattern in spring?
ans-1:-single design pattern
2:-factoryDesign pattern
3:-mvc design pattern
4:-dao design pattern


Q-spring mvc?
ans-spring mvc is design pattern which is used for seperation between presentation and business layer

In this pattern M is Model, V is the View and C is controller.   The advantage of design pattern is to separate the different responsibilities into 3 parts when dealing UI.   In general terms  Model represents the business data and logic, View is visual part where the data in Model is displayed and Controller is the link of Model and View.  Controller is the brain which decides what data is used and how data is viewed.    Each part in MVC has the clear and dedicated responsibly.   By using MVC pattern the applications with UI become easier to develop, modify and maintain. 










DisptacherServlet

DispatcherServlet is the front controller in Spring MVC.   It is a HttpServlet that will receive the request and return the response.
 
Add caption
                        
 

No comments:

Post a Comment