【源码分析设计模式 9,springboot项目实战视频

public class PeanutSoyaMilk extends SoyaMilk {

@Override

void addCondiments() {

    System.out.println(" 加入上好的花生 ");

}

}




3、红豆类 

------



package com.guor.template;

public class RedBeanSoyaMilk extends SoyaMilk {

@Override

void addCondiments() {

    System.out.println(" 加入上好的红豆 ");

}



public static void main(String[] args) {

    //制作红豆豆浆

    System.out.println("----制作红豆豆浆----");

    SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk();

    redBeanSoyaMilk.make();



    System.out.println("----制作花生豆浆----");

    SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk();

    peanutSoyaMilk.make();

}

}




 4、控制台输出

--------



![](https://img-blog.csdnimg.cn/20200610233619846.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1b3J1aV9qYXZh,size_16,color_FFFFFF,t_70)



七、SpringIOC中的模板方法模式

-------------------



![](https://img-blog.csdnimg.cn/20200611140842982.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1b3J1aV9qYXZh,size_16,color_FFFFFF,t_70)



Spring中几乎所有的扩展都是用了模板方法模式,以SpringIOC为例简单说明一下。



### 1、首先定义一个接口ConfigurableApplicationContext,声明模板方法refresh。



public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {

/*声明了一个模板方法/

void refresh() throws BeansException, IllegalStateException;

}




### 2、抽象类AbstractApplicationContext实现了接口



主要实现了模板方法refresh(这个方法很重要,是各种IOC容器初始化的入口)的逻辑。



/**

  • 模板方法的具体实现

*/

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

    // Prepare this context for refreshing.

    prepareRefresh();

//注意这个方法是,里面调用了两个抽象方法refreshBeanFactory、getBeanFactory

    // Tell the subclass to refresh the internal bean factory.

    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();



    // Prepare the bean factory for use in this context.

    prepareBeanFactory(beanFactory);

    try {

//注意这个方法是钩子方法

        // Allows post-processing of the bean factory in context subclasses.

        postProcessBeanFactory(beanFactory);



        // Invoke factory processors registered as beans in the context.

        invokeBeanFactoryPostProcessors(beanFactory);

        // Register bean processors that intercept bean creation.

        registerBeanPostProcessors(beanFactory);

        // Initialize message source for this context.

        initMessageSource();

        // Initialize event multicaster for this context.

        initApplicationEventMulticaster();

//注意这个方法是钩子方法

        // Initialize other special beans in specific context subclasses.

        onRefresh();



        // Check for listener beans and register them.

        registerListeners();

        // Instantiate all remaining (non-lazy-init) singletons.

        finishBeanFactoryInitialization(beanFactory);

        // Last step: publish corresponding event.

        finishRefresh();

    } catch (BeansException ex) {

        // Destroy already created singletons to avoid dangling resources.

        destroyBeans();

        // Reset 'active' flag.

        cancelRefresh(ex);

        // Propagate exception to caller.

        throw ex;

    }

}

}




这里最主要有一个抽象方法obtainFreshBeanFactory、两个钩子方法postProcessBeanFactory和onRefresh,看看他们在类中的定义



两个钩子方法



protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {

}

protected void onRefresh() throws BeansException {

// For subclasses: do nothing by default.

}




再看看获取Spring容器的抽象方法:



/其实他内部只调用了两个抽象方法/

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {

refreshBeanFactory();

ConfigurableListableBeanFactory beanFactory = getBeanFactory();

if (logger.isDebugEnabled()) {

logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);

}

return beanFactory;

}

protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;




具体要取那种BeanFactory容器的决定权交给了子类!



### 3、AbstractRefreshableApplicationContext



AbstractRefreshableApplicationContext是实现了抽象方法getBeanFactory的子类;



public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {

@Override

public final ConfigurableListableBeanFactory getBeanFactory() {

    synchronized (this.beanFactoryMonitor) {

        if (this.beanFactory == null) {

            throw new IllegalStateException("BeanFactory not initialized or already closed - " +

                    "call 'refresh' before accessing beans via the ApplicationContext");

        }

        //这里的this.beanFactory在另一个抽象方法refreshBeanFactory的设置的

本次面试答案,以及收集到的大厂必问面试题分享:

字节跳动超高难度三面java程序员面经,大厂的面试都这么变态吗?

tion("BeanFactory not initialized or already closed - " +

                    "call 'refresh' before accessing beans via the ApplicationContext");

        }

        //这里的this.beanFactory在另一个抽象方法refreshBeanFactory的设置的

本次面试答案,以及收集到的大厂必问面试题分享:

[外链图片转存中…(img-KVesPdKk-1630929446452)]

CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值