java之将数据库数据读取到内存中

1 为了将配置信息或数据库的信息读取到内存中。可以程序启动时,启动一个servlet。在web.xml中进行配置其<load-on-startup>0</load-on-startup> 表示优先级为最大,这样就在程序启动时去加载servlet类。然后程序自动调用该servlet类的init方法
如:<servlet>
  <servlet-name>initServlet</servlet-name>
  <servlet-class>com.servlet.InitServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
 </servlet>


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<SysConfig> sysConfigLists=sysConfigService.getALlSysConfig();
if(sysConfigLists.size()>0){
for(int i=0;i<sysConfigLists.size();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:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext-*.xml</param-value>
 </context-param>


 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 


 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <servlet>
  <servlet-name>initServlet</servlet-name>
  <servlet-class>com.servlet.InitServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
 </servlet>
</web-app>






//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<SysConfig> sysConfigLists=sysConfigService.getALlSysConfig();
if(sysConfigLists.size()>0){
for(int i=0;i<sysConfigLists.size();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";
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值