The largest Interview Solution Library on the web


« Previous | 2 | | 4 | Next »

Spring Loosely Coupled Application Example

Contest.java

package com.jtc;
public interface Contest {
public String promptQuestion();
}

StrutsContest.java

package com.jtc;
public class StrutsContest implements Contest {
@Override
public String promptQuestion() {
return "Who invented Struts?";
}
}

SpringContest.java

package com.jtc;
public class SpringContest implements Contest {
@Override
public String promptQuestion() {
return "Who invented Spring?";
}
}

ContestService.java

package com.jtc;
public class ContestService {
//we are creating a generic reference
Contest contest;
public void setContest(Contest contest) {
this.contest = contest;
}
public void askQuestion() {
System.out.println(contest.promptQuestion());
}

ContestApplication.java

package com.jtc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ContestApplication {
public static void main(String[] args) {
BeanFactory beanfactory = new ClassPathXmlApplicationContext("beans.xml");
ContestService contestService = (ContestService) beanfactory.getBean("contestService");
contestService.askQuestion();
}
}

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="springContest" class="com.jtc.SpringContest">
   <bean id="strutsContest" class="com.jtc.SpringContest">
<bean id="contestService"

 class="com.jtc.SpringContest">


   

	 <property 
	 name="contest"
	 
	 <ref 
	 local="springContest"
	  />
     


	 



	  </bean>


</beans>
</beans>

Add Below Libraries
commons-logging-1.1.1
nblibraries.properties
org.springframework.asm-3.1.0.RC1
org.springframework.beans-3.1.0.RC1
org.springframework.context.support-3.1.0.RC1
org.springframework.context-3.1.0.RC1
org.springframework.core-3.1.0.RC1
org.springframework.expression-3.1.0.RC1
org-netbeans-modules-java-j2seproject-copylibstask
OutPut
Who invented Spring?
we are only changing the in beans.xml:
Who invented Struts?

Note:

This example is for loosely coupling of beans.
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com