
1. 简单工厂(非23种设计模式中的一种)
实现方式:
- 
读取bean的xml配置文件,将bean元素分别转换成一个BeanDefinition对象。 
 - 
然后通过BeanDefinitionRegistry将这些bean注册到beanFactory中,保存在它的一个ConcurrentHashMap中。 
 - 
将BeanDefinition注册到了beanFactory之后,在这里Spring为我们提供了一个扩展的切口,允许我们通过实现接口BeanFactoryPostProcessor 在此处来插入我们定义的代码。 
典型的例子就是:PropertyPlaceholderConfigurer,我们一般在配置数据库的dataSource时使用到的占位符的值,就是它注入进去的。 
 
- 
各种的Aware接口 ,比如 BeanFactoryAware,对于实现了这些Aware接口的bean,在实例化bean时Spring会帮我们注入对应的BeanFactory的实例。 
 - 
BeanPostProcessor接口 ,实现了BeanPostProcessor接口的bean,在实例化bean时Spring会帮我们调用接口中的方法。 
 - 
InitializingBean接口 ,实现了InitializingBean接口的bean,在实例化bean时Spring会帮我们调用接口中的方法。 
 - 
DisposableBean接口 ,实现了BeanPostProcessor接口的bean,在该bean死亡时Spring会帮我们调用接口中的方法。 
 
设计意义:
[非常重要]FactoryBean接口。
实现了FactoryBean接口的bean是一类叫做factory的bean。
其特点是,spring会在使用getBean()调用获得该bean时,会自动调用该bean的getObject()方法,所以返回的不是factory这个bean,而是这个bean.getOjbect()方法的返回值。
典型的例子有spring与mybatis的结合。

public Object getSingleton(String beanName){
    //参数true设置标识允许早期依赖
    return getSingleton(beanName,true);
}
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    //检查缓存中是否存在实例
    Object singletonObject = this.singletonObjects.get(beanName);
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        //如果为空,则锁定全局变量并进行处理。
        synchronized (this.singletonObjects) {
            //如果此bean正在加载,则不处理
            singletonObject = this.earlySingletonObjects.get(beanName);
            if (singletonObject == null && allowEarlyReference) {
                //当某些方法需要提前初始化的时候则会调用addSingleFactory 方法将对应的ObjectFactory初始化策略存储在singletonFactories
                ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                if (singletonFactory != null) {
                    //调用预先设定的getObject方法
                    singletonObject = singletonFactory.getObject();
                    //记录在缓存中,earlysingletonObjects和singletonFactories互斥
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }
    return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
getSingleton()过程图

SpringMVC中的适配器HandlerAdatper。
HandlerAdatper根据Handler规则执行不同的Handler。
DispatcherServlet根据HandlerMapping返回的handler,向HandlerAdatper发起请求,处理Handler。
5. 装饰器模式
实现方式:Spring中用到的包装器模式在类名上有两种表现:一种是类名中含有Wrapper,另一种是类名中含有Decorator。
实质:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
6. 代理模式
实现方式:AOP底层,就是动态代理模式的实现。
动态代理:在内存中构建的,不需要手动编写代理类
静态代理:需要手工编写代理类,代理类引用被代理对象。
实现原理:切面在应用运行的时刻被织入。一般情况下,在织入切面时,AOP容器会为目标对象创建动态的创建一个代理对象。SpringAOP就是以这种方式织入切面的。
把切面应用到目标对象并创建新的代理对象的过程。
7. 观察者模式
实现方式:spring的事件驱动模型使用的是 观察者模式 ,Spring中Observer模式常用的地方是listener的实现。
具体实现:
事件机制的实现需要三个部分,事件源,事件,事件监听器
public abstract class ApplicationEvent extends EventObject {
    private static final long serialVersionUID = 7099057708183571937L;
    private final long timestamp;
    public ApplicationEvent(Object source) {
    super(source);
    this.timestamp = System.currentTimeMillis();
    }
    public final long getTimestamp() {
        return this.timestamp;
    }
}
[事件监听器]public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
     void onApplicationEvent(E event);
}
[事件源]
职责:
public interface ApplicationEventPublisher {
        void publishEvent(ApplicationEvent event);
}
public void publishEvent(ApplicationEvent event) {
    Assert.notNull(event, "Event must not be null");
    if (logger.isTraceEnabled()) {
         logger.trace("Publishing event in " + getDisplayName() + ": " + event);
    }
    getApplicationEventMulticaster().multicastEvent(event);
    if (this.parent != null) {
    this.parent.publishEvent(event);
    }
}
[事件源中publishEvent方法需要调用其方法getApplicationEventMulticaster]public abstract class AbstractApplicationContext extends DefaultResourceLoader
    implements ConfigurableApplicationContext, DisposableBean {
    private ApplicationEventMulticaster applicationEventMulticaster;
    protected void registerListeners() {
    // Register statically specified listeners first.
    for (ApplicationListener<?> listener : getApplicationListeners()) {
    getApplicationEventMulticaster().addApplicationListener(listener);
    }
    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let post-processors apply to them!
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    for (String lisName : listenerBeanNames) {
    getApplicationEventMulticaster().addApplicationListenerBean(lisName);
    }
  }
}
8. 策略模式
实现方式:
Resource 接口介绍
- 
getInputStream(): 定位并打开资源,返回资源对应的输入流。每次调用都返回新的输入流。调用者必须负责关闭输入流。 
 - 
exists(): 返回 Resource 所指向的资源是否存在。 
 - 
isOpen(): 返回资源文件是否打开,如果资源文件不能多次读取,每次读取结束应该显式关闭,以防止资源泄漏。 
 - 
getDescription(): 返回资源的描述信息,通常用于资源处理出错时输出该信息,通常是全限定文件名或实际 URL。 
 - 
getFile: 返回资源对应的 File 对象。 
 - 
getURL: 返回资源对应的 URL 对象。 
 
- 
UrlResource: 访问网络资源的实现类。 
 - 
ClassPathResource: 访问类加载路径里资源的实现类。 
 - 
FileSystemResource: 访问文件系统里资源的实现类。 
 - 
ServletContextResource: 访问相对于 ServletContext 路径里的资源的实现类. 
 - 
InputStreamResource: 访问输入流资源的实现类。 
 - 
ByteArrayResource: 访问字节数组资源的实现类。 
 
9. 模版方法模式
经典模板方法定义:
- 
抽象方法:父类中的是抽象方法,子类必须覆盖 
 - 
钩子方法:父类中是一个空方法,子类继承了默认也是空的 
 
Spring 模板方法模式实质:
具体实现:
public abstract class JdbcTemplate {
     public final Object execute(String sql){
        Connection con=null;
        Statement stmt=null;
        try{
            con=getConnection();
            stmt=con.createStatement();
            Object retValue=executeWithStatement(stmt,sql);
            return retValue;
        }catch(SQLException e){
             ...
        }finally{
            closeStatement(stmt);
            releaseConnection(con);
        }
    }
    protected abstract Object executeWithStatement(Statement stmt, String sql);
}
引入回调原因:
public interface StatementCallback{
    Object doWithStatement(Statement stmt);
}
public class JdbcTemplate {
    public final Object execute(StatementCallback callback){
        Connection con=null;
        Statement stmt=null;
        try{
            con=getConnection();
            stmt=con.createStatement();
            Object retValue=callback.doWithStatement(stmt);
            return retValue;
        }catch(SQLException e){
            ...
        }finally{
            closeStatement(stmt);
            releaseConnection(con);
        }
    }
    ...//其它方法定义
}
JdbcTemplate jdbcTemplate=...;
    final String sql=...;
    StatementCallback callback=new StatementCallback(){
    public Object=doWithStatement(Statement stmt){
        return ...;
    }
}
jdbcTemplate.execute(callback);
   
   
   
   
   
   
 
    
    
    -END-
   
   
    
   
   
   
 
    
    
    如果看到这里,说明你喜欢这篇文章,请 转发、点赞。微信搜索「web_resource」,关注后回复「进群」或者扫描下方二维码即可进入无广告交流群。
   
   
    ↓扫描二维码进群↓
   
   
   
 
    
    
    
   
   
    
   
   
   
   
   
   
 
    
    
    推
 
    
    
    荐
 
    
    
    阅
 
    
    
    读 
   
   
    
   
   
   
 
    
    
    1.  一份 Spring Boot 项目搭建模板
   
   
    
   
   
   
 
    
    
    2. 
 
    
    
     Spring Boot 实现应用监控和报警
   
   
    
   
   
   
 
    
    
    3.  Nginx 从入门到实战
   
   
    
   
   
   
 
    
    
    4.  一键式搭建分布式文件服务器
   
   
    5.  团队开发中 Git 最佳实践

   
   
   
 
    
    
    喜欢文章,点个
 
    
    
    在看 
 
    
    
    
   
   
    
         
         
         
 
          
          
          
         
         
          
本文分享自微信公众号 - Java后端(web_resource)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
相关文章
暂无评论...
