您的位置:首页 > 运维架构 > Tomcat

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

2016-12-01 15:50 357 查看

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 !");
}
...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: