如何让一段代码tomcat启动就运行

本文介绍了一种通过监听器实现在线会话管理的方法,并详细展示了如何配置监听器来实现用户离开时移除其在线状态的功能。此外,还介绍了如何将相关服务配置为Spring的bean,并确保在应用启动时正确初始化。

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

1. 在 web.xml 里面配置为 listener

<listener>
        <listener-class>com.pandaroid.uniqueonline.UniqueOnlineSessionListener</listener-class>
</listener>
public class UniqueOnlineSessionListener implements HttpSessionListener {
    static {
        System.out.println("haha static");
    }
    @Override
    public void sessionCreated(HttpSessionEvent arg0) {  
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        UniqueOnlineRecords.removeUser(arg0.getSession());
    }

}

2. 配置为 Spring 的 bean

使用这种方式时,bean 的 scope 必须设为默认的 singleton 。因为只有这样,Spring 才会在应用服务器启动时为我们加载相应的 bean 。

<bean id="HiReportService" class="com.pandaroid.hiReport.service.HiReportServiceImpl">
     <property name="fileUploadDiskPath" ref="fileupload.disk.path"/>
     <property name="fileUploadHttpPath" ref="fileupload.http.path"/>
     <property name="jasperService" ref="JasperService"/>
 </bean>
public class HiReportServiceImpl implements HiReportService {
     static {
         System.out.println("Spring bean static !");
     }
     ...
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值