Monday, April 3, 2017

Explain about spring container in Spring IoC JavahighQ

         Explain about spring container?

     BeanFactory :-

If we have two beanfactory containers it the application, we con set one BeanFactory into another beanfactory to allow the beans in one beanfactory to refer to the beans of other factory in this we can declare one beanFactory as a parent and other as a child. This is similar to concept of base class and derived classes derived class can access the properties of base class but base classes cannot access the properties of derived class.
In order to refer to parent beans we can use tag<ref parent=””/>
Apart from parent attribute it has local which indicates refer to the local first if not found then search in parent factory and perform injection.

        ApplicationContext  Container:-

          It is an enhancement for BeanFactory container . To activate ApplicationContext container  we can create object fro the java class that implements ApplictionContext interface. This is sub interface of Beanfactory(I).

There are three important implementations classes for ApplicationContext(I)
Thery are  :-
          
       a)      FileSystemXmlApplictionContext
       b)     ClassPathXmlApplictionContext
       c)      XmlWebApplictionContext
       d)     AnnotatinConfigApplicationContext

Note :-  

ApplicationContext container is originally of spring context/spring JEE module.
ApplicationContext container can erform every activity of BeanFacotry container but it also gives the following additional features.
         
           1.       Ability to work with multiple spring cfg flies
           2.       Pre-Instantiation of singleton class
           3.       Ability to working property files.
           4.       Support to work with 18n(internationalization).

           5.       Ability to work with event listeners 

        By activating BeanFactory container fo one time we con’t make that container dealing multiple spring configuration files but this activity is possible by using ApplicationContext container. 



What is bean inheritance in Spring JavahighQ

          What is bean inheritance?


          We use inheritance for reusability and extensibility. Bean inheritance is no way related with class level physical inheritance. It is all about xml file level ban configurations inheritance.

When we want configure same class for multiple times as multiple spring beans we can use reuse of bean properties configuration across the multiple beans configurations by using bean inheritance.

Sunday, April 2, 2017

What is autowiring in Spring

                    What is autowiring   ?

Autowairing:-

Auto wiring means automatically injecting the dependencies. (Implicit Dependency Injection)
Insead of manually configuring (Explicit DIO) the injection we can do automatically by using autowiring.
To implements autowiring we use “autowire” attribute in<bean> tag configuration.
To perform autowiring for a particular bean we can use any one of the following values.

Types of Autowiring:-

            a)      byName
            b)     byType
            c)      constructor
            d)     autodetect

By using autowire attribute auto wiring is possible only on reference type bean prototypes that mean auto wiring is not possible on simple collection framework type properties.
Auto wiring kills the readability of spring configuration file.
     
byName:-

Mainly it checks for three conditions if all these ate valid then it injects the values by setter approach
       
           1.       dependency bean name
           2.       configured bean name
           3.       setter method name

è it dependency name is abc,bean configuration shoud be abc and the setter name method shoud i.e setAbc(Abc abc)

è when it finds autowire=”byName” for any bean confuguratin the it first checks for dependency bean nam in the dependent bean.

è Then it will checks weathdr any bean is configured in the sprng  cfg file with the same name.

è If it finds then it will call corresponding setter method of dependent bean.

byType :-

Mainly it checks for three conditions if all these are valid then it injects the values by setter approach
       
           1.       Dependency bean name
           2.       configured bean name
            3.       setter method argument type

è When it finds “autowire=”byType” for any bean configuration then it first checks for dependency bean type in the dependent bean then after it will checks weather any bean is configured in the spring cfg file with the same type. If it find then it will call corresponding setter method.

Constructors:-

Mainly it checks for three conditions if all these are valid then it injects the values by setter approach
1.       Dependency bean name
2.       configured bean name
3.       constructor argument type

è When it finds “autowire=” constructor” for any bean configuration then it first checks for dependency bean type in the dependent bean then after it will checks weather any bean is configured in the spring cfg file

è Weather any bean is configured with the type. If it finds check for constructor


è Which will talks dependency type if it finds it will check for constructor. 

Explain difference scopes of beans in Spring

            Explain difference scopes of beans?
We can make IOC container keeping out spring bean class  object in different scopes. They are
         a)      Singleton (default scope is Singleton)
         b)     Prototype
         c)      Session
         d)     Request
         e)      Globalsession (globalsession is removed in spring 3.x version)

 Singleton:  returns same bean class object for all factory.getBean(-) calls with same bean id.
Prototype: returns separate bean class object for every factory.getBean(-) method call.

Request: Bean class object is specific to each request.

Session: Bean class object is visible throughout session

GlobalSession : specific to port lets  environments
Use “scope” attributes of <bean> tag to specify bean scope.

Note:-
Request, session scopes can be used only in spring MVC applications.


Eg: - applicationContext.xml

Saturday, April 1, 2017

Difference between Setter injection & constructor injection in Spring

               Difference between Setter injection & constructor injection?


è If bean class contains only one property or if all the property of bean class should participate in DI then go for constructor injection ,because construct injection is very faster than setter  injection

è If bean class contains more than one property and there is no need of making all properties participating in DI then we need to go for setter injection.

Eg:- 

If bean class contains four properties and there is no need of making all properties participating in DI then we need to write max of four setter methods to performs setter injection. But we need to write four factorial (24) constructors to supports to constructor injection in all angles.




Settter Injection
Constructor Injection
            1.   Performs injection after creating mean                   class     object so it is bit delay injection
       1.       Performs  injection while creating bean class oj so no delay in injection
            2.      Support cyclic dependency injection
      2.       Doesn’t supports cyclic injection
            3.      <property> tag is required.
      3.       <condtructor-args> tag is required
            4.      To preforms setter injection on “n-                         parameters”      in all angles “n-setter”                    methods are required.
      4.       To preforms constructor injection on “n-parameters” in all angles n! Constructors are required.
             5.      If all properties are configured for setter              injection then container creates bean                    class object using 0-Param constructor
      5.       If all properties are configured for constructor  injection then container creates bean class object using Parameterized  constructor

What is Dependence Injection in Spring

         What is Dependence Injection ?

è The process of injecting the dependence into dependent obj.

è If underlying server/container/framework/runtime(like JRE) assigns values to resources /class/objs dynamically at run time then it is called Dependency Injection .

è If underlying server or container dynamically assigns the dependent values to out resource (class/objects) then it is called DI.

Types of DI:-

a)      Setter Injection
b)     Constructor Injection
c)      Interface Injection
d)     Method Injection

e)      Lookup Method Injection 

What is Spring Inversion of Control (IoC) in Spring JavaHighQ

                   What is Spring Inversion of Control (IoC) in Spring
?


The spring Inversion of Control(IoC) is responsible for creating the objects, managing then (with dependency injection (DI)) wiring then together, configuring them also managing their complete lifecycle.


Inversion of Control(IoC) or dependency injection minimizes the amount of coding in an app. It makes easy to test app since no singletons of "JNDI" lookup mechanisms are required in unit tests loose coupling is promoted with minimal effort and least intrusive mechanisms. Inversion of Control(IoC) containers Support eager instantiation and lazy loading of services.