1、ApplicationListener在使用过程中可以监听某一事件的发生,可以做出相应的处理,这个方式不常用,但是在特殊情况下面还是有用的。

2、导包pom.

<project  ns="http://maven.apache.org/POM/4.0.0"  ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.troy</groupId>  <artifactId>springInit</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <dependencies>      <dependency>      <groupId>org.spring work</groupId>      <artifactId>spring-webmvc</artifactId>      <version>3.2.8.RELEASE</version>    </dependency>  </dependencies></project>

3、web. 配置

<?  version="1.0" encoding="UTF-8"?><web-app  ns:xsi="http://www.w3.org/2001/ Schema-instance"  ns="http://java.sun.com/ /ns/javaee" xsi:schemaLocation="http://java.sun.com/ /ns/javaee http://java.sun.com/ /ns/javaee/web-app_2_5.xsd" version="2.5">  <display-name>springInit</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <servlet>      <servlet-name>springmvc</servlet-name>      <servlet-class>org.spring work.web.servlet.DispatcherServlet</servlet-class>      <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:spring/spring-mvc. </param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping>  <filter>      <filter-name>encodingFilter</filter-name>      <filter-class>org.spring work.web.filter.CharacterEncodingFilter</filter-class>      <init-param>          <param-name>encoding</param-name>          <param-value>UTF-8</param-value>      </init-param>      <init-param>          <param-name>forceEncoding</param-name>          <param-value>true</param-value>      </init-param>  </filter>  <filter-mapping>      <filter-name>encodingFilter</filter-name>      <url-pattern>/</url-pattern>  </filter-mapping></web-app>

4、spring-mvc. 配置

<?  version="1.0" encoding="UTF-8"?><beans  ns="http://www.spring work.org/schema/beans"      ns:xsi="http://www.w3.org/2001/ Schema-instance"     ns:context="http://www.spring work.org/schema/context"      ns:jdbc="http://www.spring work.org/schema/jdbc"       ns:jee="http://www.spring work.org/schema/jee"      ns:tx="http://www.spring work.org/schema/tx"     ns:aop="http://www.spring work.org/schema/aop"      ns:mvc="http://www.spring work.org/schema/mvc"     ns:util="http://www.spring work.org/schema/util"     ns:jpa="http://www.spring work.org/schema/data/jpa"    xsi:schemaLocation="        http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans-3.2.xsd        http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-3.2.xsd        http://www.spring work.org/schema/jdbc http://www.spring work.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.spring work.org/schema/jee http://www.spring work.org/schema/jee/spring-jee-3.2.xsd        http://www.spring work.org/schema/tx http://www.spring work.org/schema/tx/spring-tx-3.2.xsd        http://www.spring work.org/schema/data/jpa http://www.spring work.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop-3.2.xsd        http://www.spring work.org/schema/mvc http://www.spring work.org/schema/mvc/spring-mvc-3.2.xsd        http://www.spring work.org/schema/util http://www.spring work.org/schema/util/spring-util-3.2.xsd">    <mvc:annotation-driven/>    <context:component-scan  -package="com.troy"/>    <bean class="com.troy.springInit.SpringListenerEvent"/>    <bean class="com.troy.springInit.SpringListener"/>    <bean id="initApplicationContext" class="com.troy.springInit.InitApplicationContext"/></beans>

5、<bean class="com.troy.springInit.SpringListener"/>在spring的作用是为了在bean加载完成过后进行初始化加载
  这里可以参考:http://www.cnblogs.com/ll409546297/p/6903357.html

6、目录结构,因为我用初始化加载来实现某一个事件的监听,自己可以根据相应事件触发来实现

7、触发方式

  1)第一步:因为事件触发需要用到ApplicationContext的publishEvent来触发事件,我这里没有ClassPath ApplicationContext的方式来加载配置文件,而是实现ApplicationContextAware这个接口来实现ApplicationContext的加载

public class InitApplicationContext implements ApplicationContextAware{        public static ApplicationContext ac;        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {              this.ac = applicationContext;           }  }

  2)第二步编写事件:继承ApplicationEvent类会要求重写一个构造方法

public class InitEvent extends ApplicationEvent{    public String data;    public InitEvent(  source) {               super(source);                   }    public InitEvent(  source,String data) {               super(source);            this.data = data;    }    public String getData() {        return data;    }    public void setData(String data) {        this.data = data;    }           //这里我是利用spring初始化来实现方法的加载    public static void execute(){        ApplicationContext ac = InitApplicationContext.ac;        System.out.println("test==================test");        InitEvent ie = new InitEvent("test", "test");        ac.publishEvent(ie);    }    }

  3)通过初始化的方式来触发这个事件,这个可以通过其他方式,比如servlet等方式来触发

public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{        public void onApplicationEvent(ContextRefreshedEvent event) {        System.out.println(event.getApplicationContext());        //判断spring容器是否加载完成        if (event.getApplicationContext().getParent() == null) {            Init.init();            InitEvent.execute();        }    }}

  4)上面的方式也可以触发事件,但是写出来比较繁琐。我们一般通过ApplicationListener接口来实现事件的监听,这样来触发事件的产生。通过ApplicationListener的泛型机制,来监听事件的发生,这里可以针对于时间做相对处理,实现对事件的监听

public class SpringListenerEvent implements ApplicationListener<InitEvent>{    public void onApplicationEvent(InitEvent event) {        System.out.println("事件处理==================事件处理");        System.out.println(event.getData());            }}

8、总体来说:ApplicationListener是对相应应用的监听,只是对于不同方式,通过不同方式来呈现!

 

收藏 打印