一谈到spring,它可是集设计之大成,今天我们来浅谈一下spring中的工厂模式。第一次写关于设计模式的文章,如有纰漏,还请指正。
当然这只是冰山之一角。各位如果觉得太简单,只当路过而已
。
我们都知道工厂模式是GOF23中设计模式之一,属于创建模式的一种。
而工厂模式又细分为工厂方法和抽象工厂。
简单的来说,工厂方法直接在工厂里返回所需求的实例。针对只创建一种“产品”时使用。
而抽象方法则是用在创建多个“产品”的情况下,把创建“产品”的过程延迟到了子类中,只留下了一个抽象的创建方法。
下面我们来看一下spring中ApplicationContext及子类的对于工厂方法的使用。
我们先来看一看ClassPathXmlApplicationContext的继承结构:
在抽象类AbstractRefreshableApplicationContext中有getBeanFactory()的定义
/**
* Subclasses must return their internal bean factory here. They should implement the
* lookup efficiently, so that it can be called repeatedly without a performance penalty.
* <p>Note: Subclasses should check whether the context is still active before
* returning the internal bean factory. The internal factory should generally be
* considered unavailable once the context has been closed.
* @return this application context's internal bean factory (never <code>null</code>)
* @throws IllegalStateException if the context does not hold an internal bean factory yet
* (usually if {@link #refresh()} has never been called) or if the context has been
* closed already
* @see #refreshBeanFactory()
* @see #closeBeanFactory()
*/
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
当然,此方法是其实现的接口ConfigurableApplicationContext中的一个方法。如下:
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext, DisposableBean {
...
}
我们来看看ConfigurableApplicationContext 的定义
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle {
...
ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
}
而getBeanFactory()方法却在AbstractRefreshableApplicationContext类中被大量的使用了,随便举一二例:
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
...
}
又如:
protected void initLifecycleProcessor() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
...
}
而直到其子类AbstractRefreshableApplicationContext中,才得以实现:
@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");
}
return this.beanFactory;
}
}
- 大小: 12.3 KB
分享到:
相关推荐
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
spring源码spring-framework-4.3.2.RELEASE
spring源码编译之后,各个项目可能缺失jar包 spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.5.1.jar
官方源码 spring-framework-5.3.4.zip官方源码 spring-framework-5.3.4.zip
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单...
官方原版源码spring-framework-5.1.4.RELEASE.zip
官方源码 spring-framework-5.3.2.zip官方源码 spring-framework-5.3.2.zip
官方源码 spring-framework-5.2.15.RELEASE.zip官方源码 spring-framework-5.2.15.RELEASE.zip
Spring源码缺失的spring-cglib-repack-3.2.6.jar和spring-objenesis-repack-2.6.jar
官方原版源码spring-framework-5.1.6.RELEASE.zip
Spring 源码分析文档----自用。
spring源码导入SpringObjenesis类报错解决,缺少jar包spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar
Spring源码编译缺少的两个包:spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.2.jar
spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()
spring-framework-4.2.5.RELEASE,spring4.2.5版源码。
spring-security-web源码 所需jar包
官方原版源码spring-framework-4.3.22.RELEASE.zip
spring4.0源码编译时缺失的两个jar包,spring-cglib-repack-3.1.jar,spring-objenesis-repack-2.1.jar
官方原版源码spring-framework-5.1.9.RELEASE.zip