<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
        /**
         * Handle an application event.
         * 
    @param event the event to respond to
         
    */
        void onApplicationEvent(E event);
     
    }
    下面舉個例子
    自定義事件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;
        }
    }

    定義監聽器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("郵件內容:" + event.getContent());
        }
    }

    單元測試類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 閱讀(1281) 評論(0)  編輯  收藏 所屬分類: spring
    <2020年4月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    Java

    搜索

    •  

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: a毛片在线看片免费| 男女拍拍拍免费视频网站| 久久精品人成免费| 国产亚洲精品观看91在线| 精品免费久久久久国产一区| 四虎永久在线精品免费影视 | 国产精品色午夜视频免费看| 亚洲中文字幕无码中文| 成人爽A毛片免费看| 亚洲日本VA中文字幕久久道具| 午夜神器成在线人成在线人免费 | 亚洲精品无码久久毛片| 四虎一区二区成人免费影院网址| 国产一级淫片免费播放| 一级做a爰片性色毛片免费网站| 久久久久亚洲av成人无码电影| 在线观看黄片免费入口不卡| 亚洲av永久无码精品表情包| 亚洲免费二区三区| 亚洲成年网站在线观看| 亚洲 另类 无码 在线| 91在线免费观看| 亚洲美女一区二区三区| 免费的一级黄色片| 一区二区三区视频免费观看| 亚洲av无码专区在线播放| 丁香花免费完整高清观看| 婷婷国产偷v国产偷v亚洲| 国产亚洲AV夜间福利香蕉149| 日本视频免费高清一本18| 亚洲人成777在线播放| 免费一级做a爰片性色毛片| 人人玩人人添人人澡免费| 亚洲免费二区三区| 亚洲精品国产电影| 无码免费一区二区三区免费播放| 亚洲午夜精品一区二区麻豆| 国产精品亚洲成在人线| 午夜高清免费在线观看| 国产一级淫片a免费播放口| 亚洲熟伦熟女专区hd高清|