Listener中注入Service

本文介绍了一个Spring Web应用中的ServletContextListener实现示例,该监听器在Tomcat启动时初始化特定的服务Bean。文章展示了如何通过WebApplicationContextUtils获取应用上下文,并从中获取所需的VehicleLoadDataService服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.rratchet.scala;


import com.rratchet.scala.entity.VehicleLoadDataEntity;
import com.rratchet.scala.server.MinaServer;
import com.rratchet.scala.service.DllServiceUtils;
import com.rratchet.scala.service.VehicleLoadDataService;
import com.rratchet.scala.util.Const;
import org.apache.log4j.Logger;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

public class ServerListener implements ServletContextListener {

	private static Logger logger = Logger.getLogger(MinaServer.class);

	private TestService testService;

    @Override
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	// tomcat启动后就会执行该方法
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
	
		
		try {
			XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
			testService= (VehicleLoadDataService) cxt.getBean("testService");
			
		}catch (Exception e){
			logger.info(">>>>>>>>>>>>>:"+e.getMessage());
		}

	}

	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值