spring中所有使用到设计模式的地方有哪些,并给出代码示例
时间: 2024-05-06 12:20:22 浏览: 126
Spring中使用到的设计模式有:
1. 单例模式:Spring默认所有的bean都是单例的,保证了在整个应用中只有一个实例。
2. 工厂模式:Spring使用工厂模式创建和管理bean,可以通过BeanFactory和ApplicationContext获取bean。
3. 代理模式:Spring使用代理模式实现AOP,通过代理对象来增强目标对象的功能。
4. 模板方法模式:Spring中的JdbcTemplate、HibernateTemplate等都是基于模板方法模式实现的。
5. 观察者模式:Spring中的事件驱动模型就是基于观察者模式实现的,通过ApplicationContext发布事件,然后监听器接收事件并处理。
6. 适配器模式:Spring中的适配器模式主要应用在MVC框架中,通过适配器来将不同的请求适配到不同的处理器上。
代码示例:
1. 单例模式
```java
@Component
public class SingletonExample {
private static SingletonExample instance;
private SingletonExample() {}
public static SingletonExample getInstance() {
if (instance == null) {
instance = new SingletonExample();
}
return instance;
}
}
```
2. 工厂模式
```java
public interface MyBean {
void sayHello();
}
@Component("myBean1")
public class MyBeanImpl1 implements MyBean {
@Override
public void sayHello() {
System.out.println("Hello from MyBeanImpl1");
}
}
@Component("myBean2")
public class MyBeanImpl2 implements MyBean {
@Override
public void sayHello() {
System.out.println("Hello from MyBeanImpl2");
}
}
public class MyBeanFactory {
public static MyBean getMyBean(String beanName) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
return (MyBean) context.getBean(beanName);
}
}
```
3. 代理模式
```java
@Component
public class MyServiceProxy implements MyService {
@Autowired
private MyService myService;
@Override
public void doSomething() {
System.out.println("Before doSomething...");
myService.doSomething();
System.out.println("After doSomething...");
}
}
@Component
public class MyServiceImpl implements MyService {
@Override
public void doSomething() {
System.out.println("doSomething...");
}
}
```
4. 模板方法模式
```java
@Component
public class MyJdbcTemplate extends JdbcTemplate {
@Autowired
public MyJdbcTemplate(DataSource dataSource) {
super(dataSource);
}
public List<Map<String, Object>> query(String sql) {
return super.queryForList(sql);
}
}
```
5. 观察者模式
```java
public class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("Received MyEvent: " + event.getSource());
}
}
@Component
public class MyEventPublisher {
@Autowired
private ApplicationContext context;
public void publishEvent() {
context.publishEvent(new MyEvent(this));
}
}
```
6. 适配器模式
```java
public interface MyController {
void handleRequest(HttpServletRequest request, HttpServletResponse response);
}
public class MyController1 {
public void handleRequest1(HttpServletRequest request, HttpServletResponse response) {
// handle request
}
}
public class MyController2 {
public void handleRequest2(HttpServletRequest request, HttpServletResponse response) {
// handle request
}
}
public class MyHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof MyController);
}
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
MyController controller = (MyController) handler;
controller.handleRequest(request, response);
return null;
}
@Override
public long getLastModified(HttpServletRequest request, Object handler) {
return -1;
}
}
```
阅读全文