面试时,初中级Java程序员一般都会问到的问题:Spring用到了哪些设计模式?
其实只要Spring使用得够熟练,回答这道题还是非常轻松的。因为Spring的命名非常规范,基本上从类名就可以看得出来用到了哪些设计模式。常用的有8种设计模式在Spring中的应用:
工厂模式
Spring的BeanFactory类,就是使用了简单工厂模式。它主要提供getBean()方法,用来创建对象的实例;见得比较多的ApplicationContext也是继承自BeanFactory。
public interface BeanFactory {
Object getBean(String name) throws BeansException;
<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
//省略...
}
单例模式
Spring中的Bean默认为
singleton
单例。我们可以通过配置Bean的作用域scope参数来进行修改。Spring Bean一共有5种内置的作用域,分别是
singleton、prototype、request、session、globalSession。
对于单例bean的创建方式,主要看DefaultSingletonBeanRegistry 的 getSingleton() 方法:
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
/** 保存单例Objects的缓存集合ConcurrentHashMap,key:beanName --> value:bean实例 */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
//检查缓存中是否有实例,如果缓存中有实例,直接返回
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
//省略...
try {
//通过singletonFactory获取单例
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
//省略...
if (newSingleton) {
addSingleton(beanName, singletonObject);
}
}
//返回实例
return singletonObject;
}
}
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
装饰器模式
在Spring中,只要见到以Wrapper命名的类基本都是使用装饰器模式。比如BeanWrapper,用来访问Bean的属性和方法。
策略模式
Spring中Bean的实例化采用的就是策略模式。因为Bean的实例化包含原生对象的实例化,和代理对象的实例化,不同对象实例化的逻辑也不一样,所以实例化策略也不一样,比如SimpleInstantiationStrategy就是Spring中默认的实例化策略。
适配器模式
在Spring,只要是以Adapter命名的类基本都是适配器模式的应用。比如MVC模块中的HandlerAdapter。
代理模式
比如AOP模块中的AopProxy,用到了JDK的动态代理和CGLIB字节码生成技术;
另外,我们再看看DefaultAopProxyFactory的createAopProxy()方法,Spring通过此方法创建动态代理类:
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
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.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
}
从源码中可以看出,Spring会先判断是否实现了接口,如果实现了接口就使用JDK动态代理,如果没有实现接口则使用Cglib动态代理,也可以通过配置,强制使用Cglib动态代理,配置如下:
<aop:aspectj-autoproxy proxy-target-class="true"/>
JDK动态代理和Cglib动态代理的区别:
JDK动态代理只能对实现了接口的类生成代理,没有实现接口的类不能使用。
Cglib动态代理即使被代理的类没有实现接口,也可以使用,因为Cglib动态代理是使用继承被代理类的方式进行扩展。
Cglib动态代理是通过继承的方式,覆盖被代理类的方法来进行代理,所以如果方法是被final修饰的话,就不能进行代理。
模板方法模式
主要用来解决代码重复的问题。Spring提供了非常多的模板类来减少重复代码,基本都是以Template结尾,比如RestTemplate,JmsTemplate,JdbcTemplate。
观察者模式
主要用于当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知,在Spring中一般以Listener结尾,比如ApplicationListener等等。