Wednesday, 25 September 2013

Spring: Application Context

This Session Talks about the Flavors of Application Context,
  • ClassPathXmlApplicationContext : Loads Context Definition From XML File Located in Classpath
    • ApplicationContext context=new
      ClassPathXmlApplicationContext("bean.xml");
  • FileSystemXmlApplicationContext : Loads from an xml loacted in filesystem (absolute/relative path should must be mentioned)
    • ApplicationContext context=new
      FileSystemXmlApplicationContext("c:/bean.xml");
  • XmlWebApplicationContext : Loads From xml file existing with in web application context.
    • ApplicationContext context=new
      XmlWebApplicationContex("bean.xml");
  • Consider Following xml Bean definition,
    • <bean name="hello" class="org.srinidhi.beans.Helloworld">
    • getBean() method of context will actually get the instantiated object of Helloworld,
      public static  void main(String args[]){

      ApplicationContext context = new FileSystemXmlApplicationContext("c:/bean.xml");

      Helloworld hw = (Helloworld ) context .getBean("hello"); 

      hw.sayHello();             // Calling method of Helloworld Class.

      }

      Hope You like this session.

Monday, 23 September 2013

Why Java Spring? Continued...

In Last Session one of the question stood tall and went unanswered

How Spring Identifies The Bean?

The answer to this question actually drags us to a most popular and second important feature of Spring, Dependency Injection. we will surely look into it but let us concentrate on the current question,
  1.  All Our Application Components i.e. Beans will live within Spring Container
  2. Spring comes with several container implementations that can be categorized into two distinct types.
    • Bean factories (org.springframework.beans.factory.BeanFactory interface)
    • Application contexts (org.springframework.context.ApplicationContext)
  3.  Bean Information will be placed in xml based file inside the <bean> tag and this xml file will be loaded to the spring container then container will identify these beans along with the properties set inside <bean> tag
  4. the bean tag typically look like this
    • <bean id="hello" class="com.srinidhi.beans.Helloworld"/> 
    • The <bean> element is the most basic configuration unit in Spring. It tells Spring to
      create an object for you.
  5. once you specify the bean info in xml following code can be used to load xml to spring application context
    •  ApplicationContext context = new ClassPathXmlApplicationContext(
      "hello.xml");
well this is much of a concept and also we are still pending with our previous session question of  "What are The Configuration for setting up spring???"... also there is much to talk about the variants of Application Context. 
these topics will be taken care in further sessions, thank u for reading.


Sunday, 22 September 2013

Java Spring: An Ultimate framework.



Having Bundles of Frameworks floating around, the first question which arise is "is spring really necessary???"... (Bekitta Iga)... Well, The exact answer depends on the requirements and application complexity. What Spring targets is following features which is an improvement/addition to existing Frameworks.
  1. Unleashing the Power Of Simple POJO-
    • Spring never forces  you to implement a spring specific interface or extend class. At the worst case a class may be annotated with spring annotations.
      Example:

      Without SPRING:
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public class HelloWorldBean implements SessionBean{
      public void ejbActivate(){
      }
      public void ejbPassivate(){        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      }                                              ;Methods of SessionBean Interface foces you to
      public void ejbRemove(){         ; implement these method which are out of interest
      }
      public void ejbCreate(){
      }
      public void setSessionContext(Session Contextctx){
      }
      public String sayHello(){
      return "HelloWorld";
      }

      }

      With Spring Above code can be simply written,

      public class HelloWorldBean{
      public String sayHello(){
      return "HelloWorld";                 ;;;; Only Business Logic, Rest Is Left To Spring.
      }
      }

      Isn't it Effective???!
 With This Basic and very tiny Info about Spring Framework i'm Logging off today. but with unanswered questions like "How Spring Identifies The Bean?" and "What are The Configuration for setting up spring???" also there are lot more features pending to be discussed, Hope You Like This Post. Will Provide You those in coming posts.