004_SpringBoot整合Listener

1. 通过注解扫描完成Listener组件的注册(SpringBoot整合Listener方式一)

1.1. 使用maven构建SpringBoot的名叫spring-boot-listener项目

1.2. pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-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.bjbs</groupId>
	<artifactId>spring-boot-listener</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
	</parent>

	<!-- 修改jdk版本 -->
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- springBoot的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

1.3. 编写FirstListener.java

package com.bjbs1.controller;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * springBoot整合Listener方式一 
 *
 * <listener> 
 * 	<listener-class>com.bjsxt1.listener.FirstListener</listener-class>
 * </listener>
 */
@WebListener
public class FirstListener implements ServletContextListener {
	@Override
	public void contextDestroyed(ServletContextEvent event) {

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("FirstListener...contextInitialized......");
	}
}

1.4. @WebListener注解

1.5. 编写App.java启动类 

package com.bjbs1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * springBoot整合Listener方式一, SpringBoot启动类
 */
@SpringBootApplication
@ServletComponentScan // 在springBoot启动时会扫描@WebListener, 并将该类实例化
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

1.6. 配置@ServletComponentScan注解, 在SpringBoot启动时会扫描@WebListener, 并将该类实例化

1.7. 运行项目 

2. 通过方法完成Listener组件注册(SpringBoot整合Listener方式二)

2.1. 编写SecondListener.java

package com.bjbs2.controller;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * springBoot整合Listener方式二
 */
public class SecondListener implements ServletContextListener {
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("SecondListener...contextInitialized......");
	}
}

2.2. 编写App2.java启动类

package com.bjbs2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.bjbs2.controller.SecondListener;

/**
 * SpringBoot整合Listener方式二, SpringBoot启动类
 */
@SpringBootApplication
public class App2 {
	public static void main(String[] args) {
		SpringApplication.run(App2.class, args);
	}

	/**
	 * 注册listener
	 */
	@Bean
	public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {
		ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
		return bean;
	}
}

2.3. 使用org.springframework.boot.web.servlet.ServletListenerRegistrationBean类注册Listener

2.4. 运行项目 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值