This is a simple example of String framework that uses two interfaces and three classes. Here, Executor classes used both classes StringProducer and String Consumer. Executor class gets both classes's instances using Spring framework and invokes method from each other.
Consumer.java
package spring.example1;
public interface Consumer {
void consume(String s);
}
-------------------------------------------------------
StringConsumer.java
package spring.example1;
public class StringConsumer implements Consumer {
public void consume(String s){
System.out.println("DATA CONSUMED : "+s);
}
}
-------------------------------------------------------
Producer.java
package spring.example1;
public interface Producer {
String produce();
}
------------------------------------------------------
StringProducer.java
package spring.example1;
public class StringProducer implements Producer {
private String data="Test data ";
public String produce(){
return data+" : TIME - "+new java.util.Date();
}
}
------------------------------------------------------
producer-consumer.properties
producer.(class)=spring.example1.StringProducer
consumer.(class)=spring.example1.StringConsumer
------------------------------------------------------
Executor.java
package spring.example1;
//It uses dependency lookup, not dependency Injection
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
public class Executor {
public static void main(String ar[]){
DefaultListableBeanFactory bf=new DefaultListableBeanFactory();
BeanDefinitionReader reader=new PropertiesBeanDefinitionReader(bf);
ClassPathResource cpr=new ClassPathResource("spring/example1/producer-consumer.properties");
reader.loadBeanDefinitions(cpr);
Producer producer=(Producer)bf.getBean("producer");
Consumer consumer=(Consumer)bf.getBean("consumer");
consumer.consume(producer.produce());
}
}
Comments