一,写一个servlet配置到web.xml:
web.xml的加载顺序:
context –> param –> listener –>filter –servlet
值得注意的是在servlet中不能用标签来取spring中实例的bean,可以先取到servlet再通过getbean来取。方法如下.
二,如果容器用了spring框架 可以写一个spring的监听
一旦spring容器启动,则会调用其初始化的方法。注意的是新写的监听应放在spring容器监听之后。
web.xml,中的servlet配置:
<!-- 自动加载调度器 -->
<servlet>
<servlet-name>autoServlet</servlet-name>
<servlet-class>com.southgis.ibase.jobschedule.webservice.TaskLoadServlet</servlet-class>
<!-- 如果需要自动加载,加下面一句 -->
<!--数字1代表优先级-->
<load-on-startup>1</load-on-startup>
</servlet>
servlet的写法:
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.southgis.ibase.jobschedule.service.IManngerService;
@Service
public class TaskLoadServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
public TaskLoadServlet() {
super();
}
/*@Inject
@Named(IManngerService.SERVICE_BEAN_NAME)
private IManngerService mManngerService;*/
public void init(){
System.out.println("******自动加载了*******");
try {
ServletContext servletContext = this.getServletContext();
//获取spring的实列bean
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
IManngerService mManngerService=(IManngerService)ctx.getBean(IManngerService.SERVICE_BEAN_NAME);
mManngerService.AddJob(1, "job1", "2017-08-29 11:16:10",0);
mManngerService.startJob();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
spring监听在web.xml中的配置:注意先后顺序
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class> com.southgis.ibase.jobschedule.webservice.TaskLoadListen</listener-class>
</listener>
监听:
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.southgis.ibase.jobschedule.dao.IJobscheduleDao;
import com.southgis.ibase.jobschedule.entity.JobscheduleEntity;
import com.southgis.ibase.jobschedule.service.IManngerService;
public class TaskLoadListen implements ServletContextListener {
//容器启动运行
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("******自动加载了*******");
ServletContext servletContext=sce.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
/* IManngerService mManngerService=(IManngerService)ctx.getBean(IManngerService.SERVICE_BEAN_NAME);
mManngerService.AddJob(1, "job1", "2017-08-29 11:16:10",0);
mManngerService.startJob();*/
IJobscheduleDao IJ=(IJobscheduleDao)ctx.getBean(IJobscheduleDao.DAO_BEAN_NAME);
List<JobscheduleEntity> re=(List<JobscheduleEntity>) IJ.findAll();
System.out.println("******自动加载完成*******");
}
//spring容器销毁时调用
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}