Wednesday, April 5, 2017

How to get the properties file data info Spring Application

           How to get the properties file data info Spring App ?

Properties file :-
        
          The text file that maintains the entries as key value pairs is called properties file. We generally use properties file to maintain JDBC properties like dirverClassName,Url,username and password and  make them as flexible to modify without touching the source code. Spring application is added as new app to the existing project and if existing application can achieve this flexibility through xml file (Spring bean configuration file) if Spring application is added as new app to the existing project and if existing applications of that project and if existing application of that project are getting JDBC properties from properties file then are should make sure that spring application also get  JDBC properties from properties file.
 In ApplicationContext container environment we need to configure “org.springframework.beans.factory.config.PropertyPlaceHolderConfigure” class as spring bean to make container to locate given properties file and to recognize place holders in spring bean configuration file .

Place holders ---à${……………}
Each placeholder represents 1 key of the property file to get values from properties file.
DBDetails.properties(com/rg/commons)
Jdbc.driver=oracle.jdbc.driver.OracleDriver
Jdbc.url=  jdbc:oracle:thin:@lovalhost:1521:xe
db.user=system
db.pwd=manager

applicationContext.xml

<bean id=”ppc” class=”org.springframework.beans.factory.config.PropertyPlaceHolderConfigure”>
<property name=”location” value=”src/com/rg/commons/ DBDetails.properties”/>
</bean>
<bean id=”drds” class=”org.springframework.jdbc.datasource.DriverManagerDataSouce”>
<property name=”driverClassName” value=”${jdbc.driver}”/>
<property name=”url” value=”${jdbc.url}”/>
<property name=”username” value=”${jdbc.user}”/>
<property name=”password” value=”${jdbc.pwe}”}/>

</bean>

Monday, April 3, 2017

What is aware injection in spring

           What is aware injection in spring?

        Sometimes we need spring framework objects in our beans to perform some operations, for example reading ServletConfig and ServletContext parameters or to know the bean definitions loaded by the ApplicationContext. That’s why spring framework provides a bunch of * Aware interfaces that we can implant in our bean classes.
 org.springframework.beans.factory.Aware is the root marker interface for all these Aware interfaces. All of the *Aware interfaces are sub-interfaces of Aware and declare a single setter method to be implemented by the bean. Then Spring context uses setter –based dependency injection to inject the corresponding objects in the bean and make t available for our use.

Some of the important Aware interfaces are:
·         ApplicationContextAware – to inject ApplicationContext object.
·         BeanFactoryAware – to inject BeanFactory object.
·         BeanNameAware – to know the ban name defined in the configuration file.
·         ServletContextAware – to inject ServletContext object in MVC application example usage is to read context parameters and attributes.
·         servletConfigAware – to inject ServletConfig objects in MVC application. Example usage is to get servlet config parameters.

Let’s see these Aware interface usage in action by implementing few of them in a class that we will configure as spring bean.



ex:-

public class MyAwareService implements ApplicatonContextAware,BeanNameAware{
@Override
public void setApplicationContext(ApplicationCOntext ctx)throws BeansExcption {
System.out.println(“setApplicationContext called”);
System.out.println(“setapplicationcontext baean devination names=”+Arrays.toString(ctx.getBeanDefinationNames()));
}
@Override
public void setBeanName(String beanName){
System.out.println(“setBeanName called”);
System.out.println(“setBanName”” NEan Name definition in contedt =”+beanName);
}

}

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