posts - 93,  comments - 2,  trackbacks - 0
          1、ApplicationContext
          Spring的核心,Context我們通常解釋為上下文環(huán)境。ApplicationContext則是應(yīng)用的容器。 Spring把Bean(object)放在容器中,需要用就通過get方法取出來。在ApplicationContext接口的眾多實(shí)現(xiàn)類中,有3個(gè)是我們經(jīng)常用到的(見表1-1),并且使用這3個(gè)實(shí)現(xiàn)類也基本能滿足我們Java EE應(yīng)用開發(fā)中的絕大部分需求。
          表1-1 ApplicationContext接口的常用實(shí)現(xiàn)類介紹
          ClassPathXmlApplicationContext
          從類路徑ClassPath中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實(shí)例化工作。例如: //裝載單個(gè)配置文件實(shí)例化ApplicationContext容器
          ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
          //裝載多個(gè)配置文件實(shí)例化ApplicationContext容器
          String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
          ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
          FileSystemXmlApplicationContext
          從指定的文件系統(tǒng)路徑中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實(shí)例化工作。例如://裝載單個(gè)配置文件實(shí)例化ApplicationContext容器
          ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml");
          //裝載多個(gè)配置文件實(shí)例化ApplicationContext容器
          String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
          ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);
          XmlWebApplicationContext
          從Web應(yīng)用中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實(shí)例化工作。這是為Web工程量身定制的,使用WebApplicationContextUtils類的getRequiredWebApplicationContext方法可在JSP與Servlet中取得IoC容器的引用
          2、ApplicationEvent
          是個(gè)抽象類,里面只有一個(gè)構(gòu)造函數(shù)和一個(gè)長整型的timestamp。其源碼如下

          public abstract class ApplicationEvent extends EventObject {
           
              /** use serialVersionUID from Spring 1.2 for interoperability */
              private static final long serialVersionUID = 7099057708183571937L;
           
              /** System time when the event happened */
              private final long timestamp;
           
              /**
               * Create a new ApplicationEvent.
               * 
          @param source the object on which the event initially occurred (never {@code null})
               
          */
              public ApplicationEvent(Object source) {
                  super(source);
                  this.timestamp = System.currentTimeMillis();
              }
           
              /**
               * Return the system time in milliseconds when the event happened.
               
          */
              public final long getTimestamp() {
                  return this.timestamp;
              }
          }

          3、ApplicationListener

          是一個(gè)接口,里面只有一個(gè)onApplicationEvent方法。如果在上下文中部署一個(gè)實(shí)現(xiàn)了ApplicationListener接口的bean,那么每當(dāng)在一個(gè)ApplicationEvent發(fā)布到 ApplicationContext時(shí),調(diào)用ApplicationContext.publishEvent()方法,這個(gè)bean得到通知。類似于Oberver設(shè)計(jì)模式。
          其源碼如下:

          public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
              /**
               * Handle an application event.
               * 
          @param event the event to respond to
               
          */
              void onApplicationEvent(E event);
           
          }
          下面舉個(gè)例子
          自定義事件NotifyEvent:
          import org.springframework.context.ApplicationEvent;

          public class NotifyEvent  extends ApplicationEvent  {
              private String email;
              private String content;
              public NotifyEvent(Object source){
                  super(source);
              }

              public NotifyEvent(Object source,String email,String content){
                  super(source);
                  this.email = email;
                  this.content = content;
              }

              public String getEmail() {
                  return email;
              }

              public void setEmail(String email) {
                  this.email = email;
              }

              public String getContent() {
                  return content;
              }

              public void setContent(String content) {
                  this.content = content;
              }
          }

          定義監(jiān)聽器NotifyListener:
          import org.springframework.context.ApplicationListener;
          import org.springframework.context.annotation.Configuration;

          @Configuration
          public class NotifyListener implements ApplicationListener<NotifyEvent>{
              @Override
              public void onApplicationEvent(NotifyEvent event) {
                  System.out.println("郵件地址:" + event.getEmail());
                  System.out.println("郵件內(nèi)容:" + event.getContent());
              }
          }

          單元測(cè)試類ListenerTest:
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.boot.test.context.SpringBootTest;
          import org.springframework.test.context.junit4.SpringRunner;
          import org.springframework.web.context.WebApplicationContext;

          @RunWith(SpringRunner.class)
          @SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
          public class ListenerTest {
              @Autowired
              private WebApplicationContext webApplicationContext;

              @Test
              public void testListener(){
                  NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
                  webApplicationContext.publishEvent(event);
              }
          }
          posted on 2020-04-09 14:47 Terry Zou 閱讀(1292) 評(píng)論(0)  編輯  收藏 所屬分類: spring

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2020年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 灵山县| 邛崃市| 徐州市| 巨鹿县| 巴东县| 阳曲县| 宜昌市| 册亨县| 泸水县| 泗阳县| 惠安县| 长寿区| 金阳县| 南漳县| 黑龙江省| 南康市| 高淳县| 永安市| 肥东县| 山东省| 新乡县| 台州市| 酒泉市| 济阳县| 扎囊县| 运城市| 黄石市| 乌拉特中旗| 固原市| 遂宁市| 尼勒克县| 甘南县| 株洲县| 连南| 张北县| 惠安县| 湘乡市| 南靖县| 汉寿县| 安陆市| 大丰市|