工厂方法模式,
通过配置Bean的类名全路径,BeanFacory接口类,下面有实现的XmlBeanFactory类,设置完配置文件和ID,直接获取。还提供默认单例缓存功能。
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("appcxt-context.xml")); Car car=bf.getBean("car", Car.class);
|
单例模式
BeanFactory 单例的实现方法:注册表(DefaultSingletonBeanRegistry中有ConcurrentHashmap缓存这个)。如果查不到,则锁住这个hashmap,双重校验一次,再创建并存入注册表。实际在AbstractBeanFactory中调用了check了两次
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(64); public Object getSingleton(String beanName, ObjectFactory singletonFactory) { Assert.notNull(beanName, "'beanName' must not be null"); synchronized (this.singletonObjects) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); } } } }
|
代理模式
JDK动态代理,CGLIB 代理,都是动态编译
- JDK动态代理:反射原理基于接口的方法做代理,
- CGLIB: 是Encacher工具类,继承代理类,植入代理逻辑.
AspectJ 编译期代理
模板模式
Mybatis Template
观察者模式
ApplicationContextListener
适配器模式
MVC中 HandlerAdapter