您的位置:首页 > 移动开发

利用java中的观察者模式模仿spring中的ApplicationContextAware

2016-08-30 18:05 661 查看

1.spring中ApplicationContextAware的作用:

spring框架中,如果需要在一个普通的javaBean中获得ioc容器对象,其中有一种方法就是 实现ApplicationContextAware接口,ioc容器启动或者销毁,对应的bean都可以获得ioc的状态。这就是java中的观察者设计者模式。在 tomcat中大量的应用到了这种设计模式,已spring中几个典型的Bean演示观察者设计者模式.

2.利用spring的几个典型的Bean演示观察者模式:

2.1类的清单:

 2.1.1 被观察者接口:ApplicationContext

 2.1.2被观察者实现类:WebApplicationContext

 2.1.3观察者接口:ApplicationContextAware

 2.1.4观察者实现类:SimpleUrlHandlerMapping

 2.1.5客户端:client

2.2类功能的描述和类的代码(注:在设计模式中的描述)

2.2.1 ApplicationContext

package com.xinghengedu.observer;
/**
* 模仿spring中的applicationContext:被观察者
* @author issuser
*被观察者需要实现三个接口:
*注册
*
*/
public interface ApplicationContext {

/**
* 注册观察者:在spring中就是注入ioc容器
*/
void  registerObserver(ApplicationContextAware applicationContextAware);

/**
* 移除观察者
*/
void  clearObserver(ApplicationContextAware applicationContextAware);

/**
* 更新观察者的状态
*/
void notifyObserver();

/**
* 模仿ioc容器的创建过程
*/
void  onCreate();

}


2.2.2 ApplicationContextAware

package com.xinghengedu.observer;

/**
*观察者需要实现的接口
* @author issuser
*
*/
public interface ApplicationContextAware {

/**
* 向观察者中注入ioc容器
* @param applicationContext
*/
void setApplicationContext(ApplicationContext applicationContext);

}


2.2.3 WebApplicationContext

package com.xinghengedu.observer;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 被观察者的具体实现
* @author issuser
*
*/
public class WebApplicationContext implements ApplicationContext {

private Map<String, ApplicationContextAware> observers = new HashMap<String, ApplicationContextAware>();

public WebApplicationContext() {

}

@Override
public void registerObserver(ApplicationContextAware applicationContextAware) {
this.observers.put(applicationContextAware.getClass().getSimpleName(), applicationContextAware);
}

@Override
public void clearObserver(ApplicationContextAware applicationContextAware) {
this.observers.remove(applicationContextAware.getClass().getSimpleName());

}

@Override
public void notifyObserver() {
Set<String> observerNames = observers.keySet();
if (observerNames.size() > 0) {
for (String temp : observerNames) {
this.observers.get(temp).setApplicationContext(this);
}
}

}

public    void  onCreate(){

System.out.println("ioc容器正在被初始化.....");

this.notifyObserver();
}

}


2.2.4 SimpleUrlHandlerMapping

package com.xinghengedu.observer;
/**
* 观察者的具体实现
* @author issuser
*
*/
public class SimpleUrlHandlerMapping  implements ApplicationContextAware{

private ApplicationContext  applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {

System.out.println("ioc呗注入到了:SimpleUrlHandlerMapping");

this.applicationContext=applicationContext;
}

public SimpleUrlHandlerMapping(ApplicationContext applicationContext) {
applicationContext.registerObserver(this);
}

}


2.2.5 Client

package com.xinghengedu.observer;

/**
* java中的观察者模式:
* 被观察者接口:ApplicationContext
* 被观察者实现类:WebApplicationContext
* 观察者接口:ApplicationContextAware
* 观察者实现类:SimpleUrlHandlerMapping
* 客户端:client
*
* @author issuser
*
*/
public class Client {

public static void main(String[] args) {
ApplicationContext context = new WebApplicationContext();
SimpleUrlHandlerMapping SimpleUrlHandlerMapping = new SimpleUrlHandlerMapping(context);

context.onCreate();

}

}
注:以上几个类虽然为spring中常用的几个类,只是借助几个类大致的描述这种设计模式的思想,spring中的源码博大精深,希望读者不要纠结于这些细节!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: