Spring Hello World


HelloWorld.java

package com.deepsingh44.model;

    public class HelloWorld {
        
      public void helloMessage() {
      
       System.out.println("Hello World");
      
      }
    
}
    

Test.java

package com.deepsingh44.model;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class Test {
   public static void main(String[] args) {
     //Without Spring Framework Hello World
     //you need to create instance of HelloWorld class.
   
     HelloWorld h=new HelloWorld();
       
     //then you can call the property
     h.helloMessage();
 
     //With Spring Framework Hello World
     //you dont need to create instance of HelloWorld class explicit
     //you need to configure in spring_config.xml file
     //Ioc container will create instance and manage all life cycle of bean.
     //get bean object from config file you need to use ApplicationContext.
     //you need to create object of ClassPathXmlApplicationContext 
     //because file is in classpath location
     
     ApplicationContext ac=new 
     ClassPathXmlApplicationContext("spring_config.xml");
     
     Object o=ac.getBean("h");
     
     HelloWorld hh=(HelloWorld)o;
     hh.helloMessage();
     
     }
    }
    

spring_config.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        
        
      <bean id="h" class="com.deepsingh44.model.HelloWorld"></bean>
        
        
     </beans>
    

No comments: