责任链模式
org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
...
retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
...
}
public Object proceed() throws Throwable {
...
//将this作为参数传递,保证当前实例中调用链的执行
interceptorOrInterceptionAdvice).invoke(this);
...
}
工厂模式
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
...
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
} catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
...
}
装饰者模式
org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource)
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
策略模式
针对不同Resource有不同的实现,从而使用不同的Factory解析
模板设计模式
声明一个抽象方法,使方法的调用延迟到子类初始化
org.springframework.dao.support.DaoSupport#afterPropertiesSet
public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
// Let abstract subclasses check their configuration.
//模板设计模式,主要子类实现
checkDaoConfig();
// Let concrete implementations initialize themselves.
try {
//对dao初始化
initDao();
}
catch (Exception ex) {
throw new BeanInitializationException("Initialization of DAO failed", ex);
}
}
适配器设计模式
org.springframework.web.servlet.DispatcherServlet#initStrategies
protected void initStrategies(ApplicationContext context) {
//处理文件上传,如果想使用multipart,则需要添加对应解析器
initMultipartResolver(context);
//国际化,分别支持url,session,cookie三种配置
initLocaleResolver(context);
//theme主题控制页面风格,
initThemeResolver(context);
//处理映射器:Controller映射处理,可设置多个,分别根据优先级处理
initHandlerMappings(context);
//适配器,通过处理映射器得到处理,轮询适配器,查找能够处理当前HTTP请求的,从而适配当前HTTP请求
//适配器设计模式
initHandlerAdapters(context);
//异常处理器,返回一个ModelAndView
initHandlerExceptionResolvers(context);
//当Controller没有返回视图,response没有流,则通过实现了此接口的类来确定此种情况下返回viewName的约定
//支持前后缀等
initRequestToViewNameTranslator(context);
//选择view渲染
initViewResolvers(context);
//请求存储
//场景,重定向的时候,定向后依旧能使用定向前数据
initFlashMapManager(context);
}
观察者模式
org.springframework.beans.factory.parsing.ReaderContext的eventListener对象,监控每次情况
public void fireDefaultsRegistered(DefaultsDefinition defaultsDefinition) {
this.eventListener.defaultsRegistered(defaultsDefinition);
}
/**
* Fire an component-registered event.
*/
public void fireComponentRegistered(ComponentDefinition componentDefinition) {
this.eventListener.componentRegistered(componentDefinition);
}
/**
* Fire an alias-registered event.
*/
public void fireAliasRegistered(String beanName, String alias, @Nullable Object source) {
this.eventListener.aliasRegistered(new AliasDefinition(beanName, alias, source));
}
调用
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#processBeanDefinition
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
//dbHolder包含配置文件中配置的各种属性,如class,name,id之类的属性
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
//若存在默认标签的子节点下再有自定义属性,则再次对自定义标签解析
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
//对解析后的dbHolder进行注册
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
//发出响应事件,通知相关监听器,这个bean已经加载完成
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
原型模式
protected RootBeanDefinition getMergedBeanDefinition(
String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)
throws BeanDefinitionStoreException {
...
//原型模式
mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
...
}
@Override
public RootBeanDefinition cloneBeanDefinition() {
return new RootBeanDefinition(this);
}
public RootBeanDefinition(RootBeanDefinition original) {
super(original);
this.decoratedDefinition = original.decoratedDefinition;
this.qualifiedElement = original.qualifiedElement;
this.allowCaching = original.allowCaching;
this.isFactoryMethodUnique = original.isFactoryMethodUnique;
this.targetType = original.targetType;
}
代理模式
org.springframework.aop.framework.DefaultAopProxyFactory#createAopProxy
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
/*
optimize:只对cglib有效,控制cglib是否采用激进的优化策略
proxyTargetClass:true则表示cglib代理,而且表示目标类本身被代理而不是目标类的接口
hasNoUserSuppliedProxyInterfaces:是否存在代理接口
*/
//如果开启cglib激进策略,proxy-target-class=true(表示目标类本身被代理,采用cglib),存在代理接口
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
//如果代理类是接口,targetClass是代理类
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
单例模式
就不讲了
转载:https://blog.csdn.net/Nuan_Feng/article/details/105846796