标签:string more data 动作 控制 nmon 覆盖 core 技术
概述
模板方法是一种行为设计模式,它在超类(抽象类)中定义算法的框架,但允许子类覆盖算法的特定步骤而无需更改其结构。
作用
//动作类
public interface Action {
/**
* 吃
* @param name
*/
void eat(String name) ;
}
//抽象模板类
public abstract class AbstractDefaultAction implements Action {
/**
* 吃饭前的动作
* 子类必须实现的方法
*/
public abstract void before();
//模板方法
@Override
public final void eat(String name) {
before();
System.out.println("吃饭吃的是:"+name);;
after();
}
/**
* 吃饭后的动作
* 钩子方法
*/
public void after(){
System.out.println("默认实现,洗手");
}
}
public class LiSi extends AbstractDefaultAction {
private static String name ="李四";
@Override
public void before() {
System.out.println(name+";吃饭前,不洗手");
}
@Override
public void after() {
System.out.println("吃完洗碗");
}
}
public class ZhangSan extends AbstractDefaultAction {
private static String name ="张三";
@Override
public void before() {
System.out.println(name+";吃饭前,洗手");
}
}
public class Main {
public static void main(String[] args) {
Action a = new ZhangSan() ;
a.eat("香蕉");
System.out.println("-----------分割线---------");
Action b = new LiSi() ;
b.eat("苹果");
}
}
使用IDEA 双击shift查找类,进入org.springframework.context.ConfigurableApplicationContext,找到refresh() 方法,找到他的实现类org.springframework.context.support.AbstractApplicationContext,它的实现方式就是一个模板方法,源码如下:
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// 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) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
根据这个refresh的方法注释可知,这就是Spring加载资源配置文件用的,规定了整个加载流程,但是放开了流程细节交由其子类实现
标签:string more data 动作 控制 nmon 覆盖 core 技术
原文地址:https://www.cnblogs.com/tanoak/p/11897851.html