单例模式 :bean默认都是单例的
原型模式 :指定作用域为prototype
工厂模式 :BeanFactory
装饰者模式 :BeanWrapper
代理模式 :动态代理
适配器模式 :Adapter
责任链模式 :使用aop的时候会先生成一个拦截器链
观察者模式 :listener,event,multicast
委托者模式 :delegate
模板方法 :postProcessBeanFactory,onRefresh,initPropertyValue
策略模式 :XmlBeanDefinitionReader,PropertiesBeanDefinitionReader
1、单例模式
参考链接:Java——》谈谈你对单例模式的理解
使用场景:在配置文件中配置注册bean对象的时候
实现方式:设置scope的值为singleton
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.dpb.pojo.User" id="user" scope="singleton">
<property name="name" value="xx"></property>
</bean>
</beans>
2、原型模式 = 克隆模式
参考链接:Java——》谈谈你对原型模式的理解
使用场景:在配置文件中配置注册bean对象的时候
实现方式:设置scope的值为prototype,表示该bean以克隆的方式生成
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.dpb.pojo.User" id="user" scope="prototype">
<property name="name" value="xx"></property>
</bean>
</beans>
3、简单工厂模式
参考链接:Java——》谈谈你对工厂模式的理解
使用场景:Spring中通过getBean()获取对象的时候
实现方式:根据id或者name获取就是简单工厂模式
4、工厂方法模式
参考链接:Java——》谈谈你对工厂模式的理解
在Spring中我们一般是将Bean的实例化直接交给容器去管理的,实现了使用和创建的分离,这时容器直接管理对象,还有种情况是,bean的创建过程我们交给一个工厂去实现,而Spring容器管理这个工厂。在Spring中有两种实现一种是静态工厂方法模式,一种是动态工厂方法模式。
使用场景:Spring中创建bean的时候
实现方式:配置factory-method
/*
* @author :xiaoixan
* @date :2022/05/20 18:27
* @class :UserFactory
* @description :工厂类
* @version :1.0
**/
public class UserFactory {
/*
* @author :xiaoixan
* @date :2022/05/20 18:27
* @description :获取实例方法,必须是static方法
* @param :
* @return :UserBean
**/
public static UserBean getInstance() {
return new UserBean();
}
}
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 静态工厂方式配置 配置静态工厂及方法 -->
<bean class="com.dpb.factory.UserFactory" factory-method="getInstance" id="user2"/>
</beans>
5、装饰者模式 = 包装模式
参考链接:Java——》谈谈你对装饰者模式的理解
使用场景:Spring session框架中的SessionRepositoryRequestWrapper使用包装模式对原生的request的功能进行增强,可以将session中的数据和分布式数据库进行同步,这样即使当前tomcat崩溃,session中的数据也不会丢失。
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
6、代理模式
参考链接:Java——》谈谈你对代理模式的理解
使用场景:Spring中AOP
7、适配器模式
参考链接:Java——》谈谈你对适配器模式的理解
使用场景:在Spring中在AOP实现中的Advice和interceptor之间的转换就是通过适配器模式实现的。
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.adapter.AdvisorAdapter;
import org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor;
import java.io.Serializable;
class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
@Override
public boolean supportsAdvice(Advice advice) {
return (advice instanceof MethodBeforeAdvice);
}
@Override
public MethodInterceptor getInterceptor(Advisor advisor) {
MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
// 通知类型匹配对应的拦截器
return (MethodInterceptor) new MethodBeforeAdviceInterceptor(advice);
}
}
8、责任链默认
使用场景:Spring中AOP的拦截器链
9、观察者模式
观察者模式定义的是对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
使用场景:监听器,Spring中listener,比如ApplicationListener
实现方式:在各个阶段发布事件
10、委托者模式
使用场景:Spring整合Shiro,SpringSecurity的时候
具体实现:DelegatingFilterProxy
11、模板模式
模板模式的核心是父类定义好流程,然后将流程中需要子类实现的方法就抽象话留给子类实现。
我们知道jdbc的步骤是固定:
- 加载驱动
- 获取连接通道
- 构建sql语句
- 执行sql语句
- 关闭资源
在这些步骤中第3步和第4步是不确定的,所以就留给客户实现,而我们实际使用JdbcTemplate的时候也确实是只需要构建SQL就可以了。
使用场景:Spring中的JdbcTemplate类的execute()方法
实现方式:子类实现具体的方法
12、策略模式
策略模式对应于解决某一个问题的一个算法族,允许用户从该算法族中任选一个算法解决某一问题,同时可以方便的更换算法或者增加新的算法。并且由客户端决定调用哪个算法,spring中在实例化对象的时候用到Strategy模式。
使用场景:spring中在实例化对象的时候用到Strategy模式
具体实现:XmlBeanDefinitionReader,PropertiesBeanDefinitionReader