Wednesday, 8 February 2017

Spring Interview Question

1):- What is IOC and DI ?IOC(Inversion Of Controller):   Giving control to the container to get instance of object is called Inversion of Control., means instead of you are creating object using new operator, let the container do that for you.

DI(Dependency Injection):  Way of injecting properties to an object is called Dependency injection.

    We have three types of Dependency injection
        1)  Constructor Injection
        2)  Setter/Getter Injection
        3)  Interface Injection
Spring will support only Constructor Injection and Setter/Getter Injection.


2):- What is default scope of Bean ?
Singleton

3):- What is Autowire ?
1) Autowaring is the process of detecting and creating the object by its name, by its type.
2) Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
3) Autowiring can't be used to inject primitive and string values. It works with reference only.

Autowiring Mode:-

<bean id="company" class="jbt.bean.Company" autowire="byType">
               <property name="companyCEO" value="VN Gautam" />
</bean>

a) No :- 


b) byName :- 

In Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it.
In this case, spring framework attempts to find out a bean in the configuration file, whose id is matching with the property name to be wired.
If a bean found with id as property name then that class object will be injected into that property by calling setter injection.
If no id is found then that property remains un-wired, but never throws any exception.

c) byType :-
  • Autowiring byType means whenever spring finds any property to be autowired, it will search for exactly one bean of given property type in container. 
  • If Spring find one(unique bean) it will autowire it. 
  • If it doesn’t find any, no auto wiring will be done(Property will not be set). 
  • If there are more than one bean of same type in container then Spring will throw Exception that byType can not be used here.

d) Constructor :-
 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.




4):- Default autowiring ?
no 


5) How  we can Inject prototype bean in Singleton bean ?

<bean id="requestProcessor" class="com.pramati.spring.RequestProcessor">
    <property name="validator" ref="validator"/>
</bean>
<bean id="validator" scope="prototype" class="com.pramati.spring.RequestValidator"/>


With this configuration, it is expected that when ever I fetch requestProcessor from application context, it will be wired with a new validator as we declared the validator bean is of prototype scope. But this does not happen.
When the application context gets initialized, it sees that requestProcessor is a singleton bean and initializes it to the context after wiring it with all the dependencies set. So from then onwards when we request context for requestProcessor, it return the same bean every time. To solve this issue, we have 2 approaches:
  1. Lookup Method injection: For this, we have to declare the beans as follows:
    <bean id="requestProcessor" class="com.pramati.spring.RequestProcessor">
     <lookup-method name="getValidator" bean="validator"/>
</bean>
<bean id="validator" scope="prototype" class="com.pramati.spring.RequestValidator"/>
  1. Scoped Proxies: This can be implemented as:
<bean id="requestProcessor" class="com.pramati.spring.RequestProcessor">
    <property name="validator" ref="validator"/>
</bean>
<bean id="validator" scope="prototype" class="com.pramati.spring.RequestValidator">
    <!-- This instructs the container to proxy the current bean-->
    <aop:scoped-proxy/>
</bean>




Q6):- Why be add @transactional at service layer why not at DAO layer or why we handle exception at service layer?


  • Ideally, Service layer(Manager) represents your business logic and hence it should be annotated with @Transactional.
  • Service layer may call different DAO to perform DB operations. Lets assume a situations where you have 3 DAO operations in a service method. 
  • If your 1st DAO operation failed, other two may be still passed and you will end up inconsistent DB state. Annotating Service layer can save you from such situations.


 Q7):- What is AOP ?
Aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification,
Example :-  "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code core to the functionality. AOP forms a basis for aspect-oriented software development.