My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Monday, November 29, 2010

SPRING with and without XML

com.check;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import
org.springframework.core.io.ClassPathResource;
import
org.springframework.core.io.Resource;
/**
* @author rkesava
* */
public
class SumRunner {public static void main(String args[]) {/* Spring IoC Without XML */
System.
System.
System.
AnnotationConfigApplicationContext context =
SummaryConfig.
GameSummary gsA = context.getBean(
System.
System.
System.
GameSummary gsP = context.getBean(
System.
System.
out.println(" ================ ");out.println(" This is to call the SPRING resources without an XML configuration" );out.println(" /* Spring IoC Without XML */ "); new AnnotationConfigApplicationContext(class);"gameSummaryBean", GameSummary.class);out.println(gsA);out.println(" ================ ");out.println(" /* Spring IoC Without XML but with Properties */ ");"gameSummaryBeanWithProperties", GameSummary.class);out.println(gsP.toString());out.println(" ================ ");/* Spring IoC with XML */
System.
System.
Resource resource =
BeanFactory beanFactory =
GameSummary gsX = (GameSummary) beanFactory.getBean(
System.
System.
System.
GameSummary gsY = (GameSummary) beanFactory.getBean(
System.
System.
}
}


package com.check;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SummaryConfig {
 @Bean
 public GameSummary gameSummaryBean() {
  return new GameSummary();
 }
 @Bean
  public GameSummary gameSummaryBeanWithProperties() {
    GameSummary gs = new GameSummary();
     gs.setClientChoice("paper");
     gs.setServerChoice("rock");
     gs.setResult("win");
     return gs;
  }
}

public
String[]
String[]
}
}
}
}
}
}
}
}
}
}
}

summary.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="gameSummaryBean" class="com.check.GameSummary" />
<bean id="gameSummaryBeanWithProp" class="com.check.GameSummary" >
<property name="clientChoice" value="paper2"/>
<property name="serverChoice" value="rock2"/>
<property name="result" value="win2"/>
</bean>
</beans>


Run it and see what difference it makes by running SPRING with an XML and without and XML.
class GameSummary {private String clientChoice, serverChoice, result;private java.util.Date date = null;choices = { "rock", "paper", "scissors" };results = { "win", "lose", "tie" };public GameSummary() {public String getClientChoice() {return clientChoice;public void setClientChoice(String clientChoice) {this.clientChoice = clientChoice;public String getServerChoice() {return serverChoice;public void setServerChoice(String serverChoice) {this.serverChoice = serverChoice;public String getResult() {return result;public void setResult(String result) {this.result = result;public java.util.Date getDate() {return date;public void setDate(java.util.Date date) {this.date = date;public String toString() {return "clientChoice = " + clientChoice + ": serverChoice = " + serverChoice + ": result=" + result + ": date=" + date;
out.println(" This is to call the SPRING resources with an XML configuration" );out.println(" /* Spring IoC with XML */ "); new ClassPathResource("summary.xml");new XmlBeanFactory(resource);"gameSummaryBean");out.println(gsX);out.println(" ================ ");out.println(" /* Spring IoC with XML with Properties */ ");"gameSummaryBeanWithProp");out.println(gsY);out.println(" ================ ");

package

No comments :