Spring Loosely Coupled Application Example
Contest.java
package com.jtc;
public interface Contest {
public String promptQuestion();
}
package com.jtc;
public class StrutsContest implements Contest {
@Override
public String promptQuestion() {
return "Who invented Struts?";
}
}
package com.jtc;
public class SpringContest implements Contest {
@Override
public String promptQuestion() {
return "Who invented Spring?";
}
}
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());
}
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();
}
}
<?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>
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