从HelloWorld到Spring

学程序大多是从HelloWorld开始 . 自己也是通过参考书看了这个例子 加上自己的一点体会. 来理解Spring 的核心

在Hello World的例子中我们有两个角色 1, 消息提供者 ,2,消息显示者.  通过分离她们的职责我们来理解Spring的 DI

temp.properties

displayer = ConcreteMessageDisplayer
supplier = HelloWorldMessageSupplier

MessageSupplier.java

public interface MessageSupplier{
 //取得消息
 String getMessage();
}

MessageDisplayer.java

public interface MessageDisplayer{
 // 消息显示接口
 void setMessageSupplier(MessageSupplier ms);
 
 MessageSupplier getMessageSupplier();
 
 void displayer();
}

HelloWorldMessageSupplier .java
public class HelloWorldMessageSupplier implements MessageSupplier{
 
 public String getMessage(){
  return "Hello world";
 }
}

ConcreteMessageDisplayer .java

public class ConcreteMessageDisplayer implements MessageDisplayer{
 public MessageSupplier  ms= null;
 
 public MessageSupplier getMessageSupplier (){
  return ms;
 }
 public void setMessageSupplier(MessageSupplier ms){
  this.ms = ms;
 }
 
 public void displayer(){
  if(ms == null){
   throw new RuntimeException("You must set the attribute of this Class "+ConcreteMessageDisplayer.class.getName());
  }else{
   System.out.println (ms.getMessage());
  }
 }
}

 

MessageSupportFactory.java

import java.util.*;
import java.io.*;

public class MessageSupportFactory{
 //Message组件生产工厂 ,单例
 
 private static MessageSupportFactory msf;
 private Properties pro;
 
 
 public synchronized static MessageSupportFactory getMessageFactory(){
  if(msf == null){
   msf = new MessageSupportFactory();
  }
  return msf;
 }
  
 private MessageSupportFactory(){
  pro = new Properties();
  try {
      pro.load(new FileInputStream("temp.properties"));
      
      
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
 }
 
 
 
 public MessageSupplier makeMessageSupplier(){
  String name = pro.getProperty("supplier");
  try {
   return  (MessageSupplier)Class.forName(name).newInstance(); 
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
  
  return null;
    
 }
 
 public MessageDisplayer makeMessageDisplayer(){
  String name = pro.getProperty("displayer");
  try {
   return  (MessageDisplayer)Class.forName(name).newInstance(); 
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
  
  return null;
 }
 
 
}

Test.java

//重构后的 Hello World

public class Test{
 public static void main(String[] args){
  //创建消息提供者
  MessageSupplier ms = MessageSupportFactory.getMessageFactory().makeMessageSupplier();
  //创建消息显示者
  MessageDisplayer md = MessageSupportFactory.getMessageFactory().makeMessageDisplayer();
  //注入消息提供者
  md.setMessageSupplier(ms);
  
  md.displayer();
  
 }
}

通过 外部配置文件+设计模式+ 反射 等来改善. 使得客户端程序员只关注MessageSupportFactory这个工厂

和 接口 并不关注消息的提供和显示.  但是还存在两个问题.

1, 工厂避免了 引用程序对具体实现子类的依赖,但是程序转而依赖工厂.

2,需要在程序手动配置部分组件. 比如  md.setMessageSupplier(ms); 注入消息提供者.

Spring 可以通过DefaultListableBeanFactory很好的解决这个问题。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值