Java監聽器Listener

一、Java監聽器java

監聽器用於監聽web應用中某些對象、信息的建立、銷燬、增長,修改,刪除等動做的發生,而後做出相應的響應處理。當範圍對象的狀態發生變化的時候,服務器自動調用監聽器對象中的方法。web

二、Java中的事件spring

Java事件由事件類和監聽接口組成,自定義一個事件前,必須提供一個事件的監聽接口以及一個事件類。
Java中監聽接口是繼承java.util.EventListener的類,事件類繼承java.util.EventObject的類。
所以Java監聽器的組成有三部分:事件源、事件監聽器、事件對象。當事件源發生操做時,它會調用事件監聽器的一個方法,而且調用這個方法時,會傳遞事件對象過來。事件監聽器是由開發人員編寫,開發人員在事件監聽器中,經過事件對象能夠拿到事件源,從而對事件源上的操做進行處理。服務器

事件監聽器接口:session

package java.util;

/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}

事件類:app

package java.util;

/**
 * <p>
 * The root class from which all event state objects shall be derived.
 * <p>
 * All Events are constructed with a reference to the object, the "source",
 * that is logically deemed to be the object upon which the Event in question
 * initially occurred upon.
 *
 * @since JDK1.1
 */

public class EventObject implements java.io.Serializable {

    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object  source;

    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
        return source;
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}

三、Java Web中定義的監聽器ide

JavaWeb開發中的監聽器(Listener)就是Application、Session和Request三大對象建立、銷燬或者往其中添加、修改、刪除屬性時自動執行代碼的功能組件。網站

ServletContextListener:對Servlet上下文的建立和銷燬進行監聽;this

ServletContextAttributeListener:監聽Servlet上下文屬性的添加、刪除和替換;spa

HttpSessionListener:對Session的建立和銷燬進行監聽。Session的銷燬有兩種狀況,一箇中Session超時,還有一種是經過調用Session對象的invalidate()方法使session失效。

HttpSessionAttributeListener:對Session對象中屬性的添加、刪除和替換進行監聽;

ServletRequestListener:對請求對象的初始化和銷燬進行監聽;

ServletRequestAttributeListener:對請求對象屬性的添加、刪除和替換進行監聽。

四、監聽器的用途:

能夠使用監聽器監聽客戶端的請求、服務端的操做等。經過監聽器,能夠自動出發一些動做,好比監聽在線的用戶數量,統計網站訪問量、網站訪問監控等。

五、自定義監聽器:

定義一個監聽用戶登陸的監聽器:

首先定義一個未登陸的監聽器:UnLoginListenser

import org.springframework.context.ApplicationListener;

/**
 * @Auther: wurong
 * @Date: 2018/7/23 19:58
 * @Description: com.shanghai.abcd.user.web.listener
 */
public class UnLoginListenser implements ApplicationListener<UnLoginEvent> {
    @Override
    public void onApplicationEvent(UnLoginEvent unLoginEvent) {
        unLoginEvent.eventMessage();
    }
}

第二步 定義一個未登陸的事件: UnLoginEvent

import com.shanghai.abcd.user.common.utils.Msg;
import com.shanghai.abcd.user.common.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;

/**
 * 未登陸事件監聽器
 * @Auther: wurong
 * @Date: 2018/7/23 19:53
 * @Description: com.shanghai.abcd.user.web.listener
 */
public class UnLoginEvent<T> extends ApplicationEvent {
    private static final Logger logger = LoggerFactory.getLogger(MyEvent.class);

    public UnLoginEvent(Object source) {
        super(source);
    }
    public Msg<String> eventMessage() {
        return ResultUtil.error(-1,"您還未登陸。");

    }
}

第三步 定義事件源:

/**
 * @Auther: wurong
 * @Date: 2018/5/24 21:28
 * @Description: com.shanghai.abcd.user.web.rest
 */

@RestController
@RequestMapping("/test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private Environment environment;

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private MyApplicationContextAware myApplicationContextAware;

    @GetMapping("/checkLogin")
    public Msg<String> checkLogin() {
        String str = "您未登陸";
        applicationContext.publishEvent(new UnLoginEvent<String>(this)); return ResultUtil.error(-1,"您未登陸");
    }

事件源就是當某一個動做發生的時候進行發佈事件,讓事件進行調用對應的監聽方法進行處理。

相關文章
相關標籤/搜索