1 为了将配置信息或数据库的信息读取到内存中。可以程序启动时,启动一个servlet。在web.xml中进行配置其
0 表示优先级为最大,这样就在程序启动时去加载servlet类。然后程序自动调用该servlet类的init方法
如:
initServlet
com.servlet.InitServlet
0
2 创建一个初始化加载的servlet时。需要在该servlet的init方法中进行初始化的工作就是把数据库的配置信息读取到内存中。
如:public class InitServlet extends HttpServlet{
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
//super.init();
System.out.println("初始化servlet的init方法");
//servlet加载完后才能使用ServletActionContext类 而此时servlet还没加载完。故无法使用ServletActionContext类 。能使用this.getServletContext()
ServletContext servletContext=this.getServletContext();//ServletActionContext.getServletContext();
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
SpringFactory.getInstance().setApplicationContext(applicationContext);
new ConfigFactory().loadSystemConfig();
new Thread(new ConfigThread()).start();
}
}
3 在servlet的init方法中,可以通过this.getServletContext()来得到servletContext对象。然后通过WebApplicationContextUtils.getWebApplicationContext(servletContext)来得到ApplicationContext对象。得到applicationContext对象的目的是为了得到bean对象(spring配置文件中的bean对象)
如:public class InitServlet extends HttpServlet{
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
//super.init();
System.out.println("初始化servlet的init方法");
//servlet加载完后才能使用ServletActionContext类 而此时servlet还没加载完。故无法使用ServletActionContext类 。能使用this.getServletContext()
ServletContext servletContext=this.getServletContext();//ServletActionContext.getServletContext();
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
SpringFactory.getInstance().setApplicationContext(applicationContext);
new ConfigFactory().loadSystemConfig();
new Thread(new ConfigThread()).start();
}
}
特别说明啊。在servlet的init方法中,在servlet还没加载完的时候。不能通过ServletActionContext.getServletContext()来得到servletContext对象。程序会报错。只能在servlet类的init方法中通过this.getServletContext()方法来得到servletContext对象。
4 在servlet的init方法中时通过线程来读取数据库信息然后保存在内存中的(先读取一次。然后通过线程一直循环来读取)。
如:new ConfigFactory().loadSystemConfig();//先读取一次
//然后线程一直循环来读取
public class ConfigThread implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new ConfigFactory().loadSystemConfig();
}
}
}
5 配置保存在内存中的类。用map来存。
如:public class ConfigFactory {
private SysConfigService sysConfigService=(SysConfigService)SpringFactory.getInstance().getBean(SpringBean.SYS_CONFIG_SERVICE);
private static Map sysConfigMap=new HashMap();
public void loadSystemConfig(){
List
sysConfigLists=sysConfigService.getALlSysConfig();
if(sysConfigLists.size()>0){
for(int i=0;i
SysConfig sysConfig=sysConfigLists.get(i);
String name=sysConfig.getName();
String value=sysConfig.getValue();
System.out.println("添加了"+name+","+value);
sysConfigMap.put(name,value);
}
}
}
}
例子:web.xml:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp
contextConfigLocation
/WEB-INF/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
initServlet
com.servlet.InitServlet
0
//InitServlet.java:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.common.SpringFactory;
import com.factory.ConfigFactory;
import com.thread.ConfigThread;
public class InitServlet extends HttpServlet{
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
//super.init();
System.out.println("初始化servlet的init方法");
//servlet加载完后才能使用ServletActionContext类 而此时servlet还没加载完。故无法使用ServletActionContext类 。能使用this.getServletContext()
ServletContext servletContext=this.getServletContext();//ServletActionContext.getServletContext();
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
SpringFactory.getInstance().setApplicationContext(applicationContext);
new ConfigFactory().loadSystemConfig();
new Thread(new ConfigThread()).start();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
//
}
}
//ConfigFactory.java:
public class ConfigFactory {
private SysConfigService sysConfigService=(SysConfigService)SpringFactory.getInstance().getBean(SpringBean.SYS_CONFIG_SERVICE);
private static Map sysConfigMap=new HashMap();
public void loadSystemConfig(){
List sysConfigLists=sysConfigService.getALlSysConfig();
if(sysConfigLists.size()>0){
for(int i=0;i
SysConfig sysConfig=sysConfigLists.get(i);
String name=sysConfig.getName();
String value=sysConfig.getValue();
System.out.println("添加了"+name+","+value);
sysConfigMap.put(name,value);
}
}
}
}
//ConfigThread.java:
package com.thread;
import com.factory.ConfigFactory;
public class ConfigThread implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new ConfigFactory().loadSystemConfig();
}
}
}
//SpringFactory.java:
package com.common;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class SpringFactory {
private ApplicationContext applicationContext;
private static SpringFactory springFactory=null;
private SpringFactory() {
super();
// TODO Auto-generated constructor stub
//ServletContext servletContext=ServletActionContext.getServletContext();
//applicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
}
public static SpringFactory getInstance(){
if(springFactory==null){
springFactory=new SpringFactory();
}
return springFactory;
}
public Object getBean(String name){
return applicationContext.getBean(name);
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
SpringBean.java:
package com.common;
public class SpringBean {
public static final String MENU_SERVICE="menuService";
public static final String SYS_CONFIG_SERVICE="sysConfigService";
}