Followers

springIOC: Hello World (XML)

HelloWorld.java
=================

package myspring;
public class HelloWorld {    
    private String message;    
    public HelloWorld(){
        System.out.println("constructor...");
    }    
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {        
        System.out.println("Metod...");
        this.message = message;
    }
    
    
}

Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloWorld" class="myspring.HelloWorld" lazy-init="true">    
        <property name="message" value="Hi Hello World, how are you? " > </property>
    
    </bean>
</beans>

Test.java
==========

package myspring;

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

public class Test {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");                          
          HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
         helloWorld.setMessage("Hi India");
        System.out.println(helloWorld.getMessage());
          
                  
    }
    
}






No comments:

Post a Comment