方法1:web服务启动时自动加载servlet
就是写一个servlet,在init()方法中写好想要执行的程序,
同时,在web.xml配置文件中指出
<servlet> <servlet-name>XXXX</servlet-name> <servlet-class>XXXX</servlet-class> <load-on-startup>1</load-on-startup></servlet>
其中粗体部分即可实现tomcat启动时执行该servlet,Jboss中同样适用~
load-on-startup的值必须为整数
当load-on-startup值为负整数或者未指定时,容器在该servlet被调用时加载
当其值为0和正整数时,容器启动时加载,值越小,优先级越高
方法2:让服务启动或停止时自动执行代码
原理是使用servlet的监听器,建立一个servlet类并且实现ServletContextListener 接口:
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TestListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { //启动时要自动执行的代码
} public void contextDestroyed(ServletContextEvent event) { //停止时要自动执行的代码
}}
在web.xml中添加一个listener ,以便让该servlet类自动执行,在web.xml中添加如下内容:
< listener > < listener-class > TestListener </ listener-class > </ listener >listener-class里面的内容就是刚才添加的servlet类
例:
web.xml如下
<!--
<!- 初始化数据库 ->
<listener>
<listener-class>main.java.com.qxbg.init.InitDataSource</listener-class>
</listener>
<!- 初始化页面模块显示的数据 ->
<listener>
<listener-class>main.java.com.qxbg.init.InitIndexShowBox</listener-class>
</listener>
<servlet>
<servlet-name>initdata</servlet-name>
<servlet-class>main.java.com.qxbg.init.Init</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
<servlet>
<servlet-name>InitialSetServlet</servlet-name>
<servlet-class>main.java.com.qxbg.init.InitIndexShowBox</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
类代码如下:
InitDataSource.java
package main.java.com.qxbg.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import net.abbp.dao.spring.manager.DataSourceManager;
/**
* 初始化数据源
*
* @version 1.0
* @author <a herf="chenbin_2008@126.com">chenbin</a>
* @since EQ 1.0
*
* <pre>
* 历史:
* 建立: Jul 17, 2012 chenbin
* </pre>
*/
public class InitDataSource implements ServletContextListener {
public InitDataSource(){
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
}
public void contextDestroyed(ServletContextEvent arg0) {
}
public static void main(String[]s){
new InitDataSource();
}
}
InitIndexShowBox.java
package main.java.com.qxbg.init;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import main.java.com.qxbg.model.entity.Content;
import main.java.com.qxbg.utils.ContentShowBox;
import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;
import net.abbp.dao.condition.DefaultCondition;
import net.abbp.dao.condition.ICondition;
import net.abbp.dao.spring.manager.DataSourceManager;
/**
* 服务器启动时初始化模块数据
*
* @version 1.0
* @author <a herf="chenbin_2008@126.com">chenbin</a>
* @since EQ 1.0
*
* <pre>
* 历史:
* 建立: Aug 1, 2012 chenbin
* </pre>
*/
public class InitIndexShowBox extends HttpServlet {
private static final long serialVersionUID = 4907822263290955712L;
private static String authorityconf = "WEB-INF/authority.txt";
public void init() throws ServletException {
ServletContext context = this.getServletContext();
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
// 用户审批信息
ContentShowBox userBox = this.initUserBox();
// 区县工作动态
ContentShowBox qxWorkBox = this.initQxWorkBox();
// 科研动态
ContentShowBox scienticBox = this.initScientic();
// 其他
ContentShowBox otherBox = this.initOther();
// 新闻信息通知
ContentShowBox newsInfoBox = this.initNewsInfo();
//存入map
Map<String,ContentShowBox> boxMap = new HashMap<String,ContentShowBox>();
boxMap.put("userBox", userBox);
boxMap.put("QxWorkBox", qxWorkBox);
boxMap.put("scienticBox", scienticBox);
boxMap.put("otherBox", otherBox);
boxMap.put("newsInfoBox", newsInfoBox);
// 将map存入Applocation
context.setAttribute("initBox",boxMap);
// context.setAttribute();
// context.setAttribute();
// context.setAttribute();
// context.setAttribute();
}
// 初始化用户审批信息
private ContentShowBox initUserBox() {
ContentShowBox box = new ContentShowBox();
// ......
// box = this.get5(list);
return box;
}
// 初始化新闻信息通知
private ContentShowBox initNewsInfo() {
ContentShowBox box = new ContentShowBox();
List<Content> list = new ArrayList<Content>();
list = this.selectBySql("新闻信息通知");
box = this.get5(list);
return box;
}
// 初始化区县工作动态
private ContentShowBox initQxWorkBox() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("区县工作动态");
box = this.get5(list);
return box;
}
// 初始化科研动态
private ContentShowBox initScientic() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("科研动态");
box = this.get5(list);
return box;
}
// 初始化其他
private ContentShowBox initOther() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("其他");
box = this.get5(list);
return box;
}
/**
* 取出最近的新闻信息
*
* @param list
* @return
*/
private ContentShowBox get5(List<Content> list) {
ContentShowBox box = new ContentShowBox();
// 获得‘容量’
int maxSize = box.getMaxSize();
for (int i = 0; i < maxSize; i++) {
box.add(list.get(i));
}
return box;
}
/**
* 根据分类条件查询出对应的数据
*
* @param clas
* @return
*/
private List<Content> selectBySql(String clas) {
List<Content> list = new ArrayList<Content>();
IDao dao = DaoFactory.INSTANCE.newDao();
ICondition icn = new DefaultCondition("clas", clas);
list = dao.query(Content.class, icn);
return list;
}
}
就是写一个servlet,在init()方法中写好想要执行的程序,
同时,在web.xml配置文件中指出
<servlet> <servlet-name>XXXX</servlet-name> <servlet-class>XXXX</servlet-class> <load-on-startup>1</load-on-startup></servlet>
其中粗体部分即可实现tomcat启动时执行该servlet,Jboss中同样适用~
load-on-startup的值必须为整数
当load-on-startup值为负整数或者未指定时,容器在该servlet被调用时加载
当其值为0和正整数时,容器启动时加载,值越小,优先级越高
方法2:让服务启动或停止时自动执行代码
原理是使用servlet的监听器,建立一个servlet类并且实现ServletContextListener 接口:
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TestListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { //启动时要自动执行的代码
} public void contextDestroyed(ServletContextEvent event) { //停止时要自动执行的代码
}}
在web.xml中添加一个listener ,以便让该servlet类自动执行,在web.xml中添加如下内容:
< listener > < listener-class > TestListener </ listener-class > </ listener >listener-class里面的内容就是刚才添加的servlet类
例:
web.xml如下
<!--
<!- 初始化数据库 ->
<listener>
<listener-class>main.java.com.qxbg.init.InitDataSource</listener-class>
</listener>
<!- 初始化页面模块显示的数据 ->
<listener>
<listener-class>main.java.com.qxbg.init.InitIndexShowBox</listener-class>
</listener>
<servlet>
<servlet-name>initdata</servlet-name>
<servlet-class>main.java.com.qxbg.init.Init</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
<servlet>
<servlet-name>InitialSetServlet</servlet-name>
<servlet-class>main.java.com.qxbg.init.InitIndexShowBox</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
类代码如下:
InitDataSource.java
package main.java.com.qxbg.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import net.abbp.dao.spring.manager.DataSourceManager;
/**
* 初始化数据源
*
* @version 1.0
* @author <a herf="chenbin_2008@126.com">chenbin</a>
* @since EQ 1.0
*
* <pre>
* 历史:
* 建立: Jul 17, 2012 chenbin
* </pre>
*/
public class InitDataSource implements ServletContextListener {
public InitDataSource(){
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
}
public void contextDestroyed(ServletContextEvent arg0) {
}
public static void main(String[]s){
new InitDataSource();
}
}
InitIndexShowBox.java
package main.java.com.qxbg.init;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import main.java.com.qxbg.model.entity.Content;
import main.java.com.qxbg.utils.ContentShowBox;
import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;
import net.abbp.dao.condition.DefaultCondition;
import net.abbp.dao.condition.ICondition;
import net.abbp.dao.spring.manager.DataSourceManager;
/**
* 服务器启动时初始化模块数据
*
* @version 1.0
* @author <a herf="chenbin_2008@126.com">chenbin</a>
* @since EQ 1.0
*
* <pre>
* 历史:
* 建立: Aug 1, 2012 chenbin
* </pre>
*/
public class InitIndexShowBox extends HttpServlet {
private static final long serialVersionUID = 4907822263290955712L;
private static String authorityconf = "WEB-INF/authority.txt";
public void init() throws ServletException {
ServletContext context = this.getServletContext();
System.out.println("InitDataSource被实例化了");
DataSourceManager.INSTANCE.initDataSources(this.getClass().getClassLoader().getResourceAsStream(
"resources/datasource.xml"));
// 用户审批信息
ContentShowBox userBox = this.initUserBox();
// 区县工作动态
ContentShowBox qxWorkBox = this.initQxWorkBox();
// 科研动态
ContentShowBox scienticBox = this.initScientic();
// 其他
ContentShowBox otherBox = this.initOther();
// 新闻信息通知
ContentShowBox newsInfoBox = this.initNewsInfo();
//存入map
Map<String,ContentShowBox> boxMap = new HashMap<String,ContentShowBox>();
boxMap.put("userBox", userBox);
boxMap.put("QxWorkBox", qxWorkBox);
boxMap.put("scienticBox", scienticBox);
boxMap.put("otherBox", otherBox);
boxMap.put("newsInfoBox", newsInfoBox);
// 将map存入Applocation
context.setAttribute("initBox",boxMap);
// context.setAttribute();
// context.setAttribute();
// context.setAttribute();
// context.setAttribute();
}
// 初始化用户审批信息
private ContentShowBox initUserBox() {
ContentShowBox box = new ContentShowBox();
// ......
// box = this.get5(list);
return box;
}
// 初始化新闻信息通知
private ContentShowBox initNewsInfo() {
ContentShowBox box = new ContentShowBox();
List<Content> list = new ArrayList<Content>();
list = this.selectBySql("新闻信息通知");
box = this.get5(list);
return box;
}
// 初始化区县工作动态
private ContentShowBox initQxWorkBox() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("区县工作动态");
box = this.get5(list);
return box;
}
// 初始化科研动态
private ContentShowBox initScientic() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("科研动态");
box = this.get5(list);
return box;
}
// 初始化其他
private ContentShowBox initOther() {
ContentShowBox box = new ContentShowBox();
List<Content> list = this.selectBySql("其他");
box = this.get5(list);
return box;
}
/**
* 取出最近的新闻信息
*
* @param list
* @return
*/
private ContentShowBox get5(List<Content> list) {
ContentShowBox box = new ContentShowBox();
// 获得‘容量’
int maxSize = box.getMaxSize();
for (int i = 0; i < maxSize; i++) {
box.add(list.get(i));
}
return box;
}
/**
* 根据分类条件查询出对应的数据
*
* @param clas
* @return
*/
private List<Content> selectBySql(String clas) {
List<Content> list = new ArrayList<Content>();
IDao dao = DaoFactory.INSTANCE.newDao();
ICondition icn = new DefaultCondition("clas", clas);
list = dao.query(Content.class, icn);
return list;
}
}