服务启动时处理业务,获取Spring Bean

在Web.xml添加Listener

	<listener>
   		 <listener-class>com.zskx.pem.network.consult.web.ServerStartListener</listener-class>
    </listener>

 业务类

public class ServerStartListener implements ServletContextListener {

	private ConsultantService consultantService;

	private UserService userService;

	@Override
	public void contextInitialized(ServletContextEvent sce) {

		ApplicationContext ac = null;
		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce
				.getServletContext());
		setUserService((UserService) ac.getBean("userService"));
		setConsultantService((ConsultantService) ac
				.getBean("consultantService"));
		try {
			getUserService().updateAllUserStatus(EnumConsultStatus.OFFLINE);
			Thread.sleep(2000);
			getConsultantService().updateAllConsultantStatus(
					EnumConsultStatus.OFFLINE);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
	}

	public ConsultantService getConsultantService() {
		return consultantService;
	}

	public void setConsultantService(ConsultantService consultantService) {
		this.consultantService = consultantService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
 

猜你喜欢

转载自yu-zhang430.iteye.com/blog/1688156
今日推荐