- 浏览: 1044584 次
- 性别:
- 来自: US
文章分类
最新评论
-
ever丶唯爱:
有activiti的视频吗?
Activiti介绍及视频教程 -
A_T_Being:
好东西,在百度盘上面,可以下载来看。
Redis介绍及视频教程 -
zhuchao_ko:
这个世界上只有两种语言 一种没人用的 一种被骂 的 我一直相信 ...
程序员将面临再次洗牌,你作何选择? -
ljmomo:
微软 手机 现在 已经不挂 诺基亚logo 。还有就是 微软 ...
程序员将面临再次洗牌,你作何选择? -
benwg:
楼主,MacOS是基于BSD系统的,真怀疑你是否从事多年的软件 ...
程序员将面临再次洗牌,你作何选择?
SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存,利用Spring托管,并完成reload功能
各位看官,请做好心理准备,这个帖子会很长,因为小弟已经做好写很长的准备。
现在说一下需求,这次的项目是做一个水产养殖相关的项目,系统开启以后很多地方要用到数据库里的字典数据,比如部门表,养殖对象表,这些数据是可变的,但是变化频率很小,所有每次用到的时候都去数据库取数据很明显的是对资源的浪费,所以想按照以前的方法把数据从数据库里读取出来,放到内存,每次用到的时候直接去内存找就可以了。想法应该是正确的,可是用到了SSH框架,做起来就有点小困难了,研究了一下午,总是是搞定。思路和代码写出来,有错误的地方请大家指正。
正式开始!中间的调错过程省去,直接如何实现。
另配一个listener,和spring同时启动,不可取。因为listener的启动机制貌似是线程,并不是按顺序一个一个启动,所有想到直接在spring的配置文件里,注册bean文件,让bean文件来执行取数据的工作,但是这个bean显然是不能使用DAO的类,因为DAO层的东西无法注入进来,所以要有个替代的东西,这个东西好难找啊,就是BeanPostProcessor接口,用类PBSTrackManagerPostProcessor实现它其中的一个方法postProcessAfterInitialization,这个方法里可以引入一个类GetDictionaryInfo,实现类的方法getAllInfo(),当getAllInfo去调用DAO层的数据时就可以了。
不说了,直接上源码:
PBSTrackManagerPostProcessor.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author ROC
* @2010年4月5日18:24:58
* 实现BeanPostProcessor接口,可以实现在Spring加载时,调取一个的对象的方法,使其能够取得实例化以后的DAO.
*
*
*
* */
public class PBSTrackManagerPostProcessor implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object obj, String s)
throws BeansException
{
if(obj instanceof GetDictionaryInfo)//GetDictionaryInfo为类名
{
((GetDictionaryInfo) obj).getAllInfo();//getAllInfo为GetDictionaryInfo的方法
}
return obj;
}
public Object postProcessBeforeInitialization(Object obj, String s)
throws BeansException
{
return obj;
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author ROC
* @2010年4月5日18:24:58
* 实现BeanPostProcessor接口,可以实现在Spring加载时,调取一个的对象的方法,使其能够取得实例化以后的DAO.
*
*
*
* */
public class PBSTrackManagerPostProcessor implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object obj, String s)
throws BeansException
{
if(obj instanceof GetDictionaryInfo)//GetDictionaryInfo为类名
{
((GetDictionaryInfo) obj).getAllInfo();//getAllInfo为GetDictionaryInfo的方法
}
return obj;
}
public Object postProcessBeforeInitialization(Object obj, String s)
throws BeansException
{
return obj;
}
}
GetDictionaryInfo.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.ArrayList;
import edu.ldu.system.preferences.feeding.DAO.TdFeedingRatesDAO;
import edu.ldu.system.preferences.product.DAO.TdProductFishDAO;
/**
* @author ROC
* 2010年4月5日18:26:55
* 实现getAllInfo方法,取得所有常用字典表里的数据
*
* */
public class GetDictionaryInfo {
@Resource
private TdFeedingRatesDAO tdFeedingRatesDAO ;
@Resource
private TdProductFishDAO tdProductFishDAO;
public GetDictionaryInfo(){
super();
}
private static WebApplicationContext springContext;
public static ServletContext _servletContext;//这个是为了取servletContext
private static GetDictionaryInfo _instance;
public static GetDictionaryInfo getInstance()
{
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
if(null == _instance)
_instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");
return _instance;
}
public static HashMap<String,ArrayList> dictionaryInfoMap = new HashMap<String, ArrayList>();
public void getAllInfo() {
System.out.println("%%%%%%%%%%系统启动加载......");
System.out.println("@@@@@@@@@@开始从数据库中取数据!");
ArrayList tdFeedingRatesList = (ArrayList)tdFeedingRatesDAO.findAll();
System.out.println("数据LIST - 1 的大小 "+tdFeedingRatesList.size());
ArrayList tdProductFishList = (ArrayList)tdProductFishDAO.findAll();
System.out.println("数据LIST - 2 的大小 "+tdProductFishList.size());
System.out.println("@@@@@@@@@@数据从数据库中全部取出!");
dictionaryInfoMap.put("tdFeedingRatesList", tdFeedingRatesList);
dictionaryInfoMap.put("tdProductFishList", tdProductFishList);
System.out.println("@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!");
}
/**
* 重新加载字典表数据!
* */
public void reloadDictionaryInfoMap()
{
System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");
dictionaryInfoMap.clear();
System.out.println("==========~~~~~~~~~~字典表MAP,清空成功!");
getAllInfo();
System.out.println("==========~~~~~~~~~~重新加载字典表成功");
}
public HashMap<String, ArrayList> getDictionaryInfoMap() {
return dictionaryInfoMap;
}
public void setDictionaryInfoMap(HashMap<String, ArrayList> dictionaryInfoMap) {
this.dictionaryInfoMap = dictionaryInfoMap;
}
}
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.ArrayList;
import edu.ldu.system.preferences.feeding.DAO.TdFeedingRatesDAO;
import edu.ldu.system.preferences.product.DAO.TdProductFishDAO;
/**
* @author ROC
* 2010年4月5日18:26:55
* 实现getAllInfo方法,取得所有常用字典表里的数据
*
* */
public class GetDictionaryInfo {
@Resource
private TdFeedingRatesDAO tdFeedingRatesDAO ;
@Resource
private TdProductFishDAO tdProductFishDAO;
public GetDictionaryInfo(){
super();
}
private static WebApplicationContext springContext;
public static ServletContext _servletContext;//这个是为了取servletContext
private static GetDictionaryInfo _instance;
public static GetDictionaryInfo getInstance()
{
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
if(null == _instance)
_instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");
return _instance;
}
public static HashMap<String,ArrayList> dictionaryInfoMap = new HashMap<String, ArrayList>();
public void getAllInfo() {
System.out.println("%%%%%%%%%%系统启动加载......");
System.out.println("@@@@@@@@@@开始从数据库中取数据!");
ArrayList tdFeedingRatesList = (ArrayList)tdFeedingRatesDAO.findAll();
System.out.println("数据LIST - 1 的大小 "+tdFeedingRatesList.size());
ArrayList tdProductFishList = (ArrayList)tdProductFishDAO.findAll();
System.out.println("数据LIST - 2 的大小 "+tdProductFishList.size());
System.out.println("@@@@@@@@@@数据从数据库中全部取出!");
dictionaryInfoMap.put("tdFeedingRatesList", tdFeedingRatesList);
dictionaryInfoMap.put("tdProductFishList", tdProductFishList);
System.out.println("@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!");
}
/**
* 重新加载字典表数据!
* */
public void reloadDictionaryInfoMap()
{
System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");
dictionaryInfoMap.clear();
System.out.println("==========~~~~~~~~~~字典表MAP,清空成功!");
getAllInfo();
System.out.println("==========~~~~~~~~~~重新加载字典表成功");
}
public HashMap<String, ArrayList> getDictionaryInfoMap() {
return dictionaryInfoMap;
}
public void setDictionaryInfoMap(HashMap<String, ArrayList> dictionaryInfoMap) {
this.dictionaryInfoMap = dictionaryInfoMap;
}
}
然后这个GetDictionaryInfo.java是个普通的类,所有要有一个servlet启动并且把取得HaspMap放到context里去
MainServlet.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.ArrayList;
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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MainServlet extends HttpServlet {
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
private static final long serialVersionUID = 1L;
// private SystemParamServicesImpl sysParamService;
private static WebApplicationContext springContext;
private ServletContext context;
public void init() throws ServletException {
// Put your code here
springContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
GetDictionaryInfo._servletContext=this.getServletContext();
context = this.getServletContext();
// GetDictionaryInfo getDictionaryInfo = new GetDictionaryInfo();
HashMap<String, ArrayList> dictionaryInfoMap = (HashMap<String, ArrayList>) GetDictionaryInfo.dictionaryInfoMap;
System.out.println("字典MAP里的数据长度为" + dictionaryInfoMap.size() + "个");
context.setAttribute("dictionaryInfoMap", dictionaryInfoMap);
System.out.println("@@@@@@@@@@系统字典表数据加载成功!");
}
/**
* Constructor of the object.
*/
public MainServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.ArrayList;
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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MainServlet extends HttpServlet {
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
private static final long serialVersionUID = 1L;
// private SystemParamServicesImpl sysParamService;
private static WebApplicationContext springContext;
private ServletContext context;
public void init() throws ServletException {
// Put your code here
springContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
GetDictionaryInfo._servletContext=this.getServletContext();
context = this.getServletContext();
// GetDictionaryInfo getDictionaryInfo = new GetDictionaryInfo();
HashMap<String, ArrayList> dictionaryInfoMap = (HashMap<String, ArrayList>) GetDictionaryInfo.dictionaryInfoMap;
System.out.println("字典MAP里的数据长度为" + dictionaryInfoMap.size() + "个");
context.setAttribute("dictionaryInfoMap", dictionaryInfoMap);
System.out.println("@@@@@@@@@@系统字典表数据加载成功!");
}
/**
* Constructor of the object.
*/
public MainServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
然后完成注册功能
applicationContext.XML
view plaincopy to clipboardprint?
<!-- 调用GetDictionaryInfo 读的 -->
<bean id="pbsTrackManagerPostProcess" class="edu.ldu.utils.PBSTrackManagerPostProcessor" />
<bean id="getDictionaryInfo" class="edu.ldu.utils.GetDictionaryInfo" />
<!-- 调用GetDictionaryInfo 读的 -->
<bean id="pbsTrackManagerPostProcess" class="edu.ldu.utils.PBSTrackManagerPostProcessor" />
<bean id="getDictionaryInfo" class="edu.ldu.utils.GetDictionaryInfo" />
配置web.xml让MainServlet.java自启动
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>edu.ldu.utils.MainServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>edu.ldu.utils.MainServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
然后就是如何在Action里取到数据了:
view plaincopy to clipboardprint?
ServletContext context = ServletActionContext.getServletContext();
HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
ServletContext context = ServletActionContext.getServletContext();
HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
又一个问题就是解决触发一个函数,让这些东西重新加载。
view plaincopy to clipboardprint?
GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();
GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();
reloadDictionaryInfoMap();实现起来时比较困难的,要在GetDictionaryInfo 取到ServletContext,而且要必须取到第一次实例的GetDictionaryInfo ,所有有一个东西很好用,就是Spring的context,取到他的方式
view plaincopy to clipboardprint?
private static WebApplicationContext springContext;
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
private static WebApplicationContext springContext;
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
这样就大功告成,跑起来以后的结果,列出控制台的输出数据:
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
%%%%%%%%%%系统启动加载......
@@@@@@@@@@开始从数据库中取数据!
Hibernate: select tdfeedingr0_.pk_id as pk1_0_, tdfeedingr0_.fishTypeId as fishTypeId0_, tdfeedingr0_.waterTemperature as waterTem3_0_, tdfeedingr0_.growthStageId as growthSt4_0_, tdfeedingr0_.baitId as baitId0_, tdfeedingr0_.feedingRate as feedingR6_0_, tdfeedingr0_.state
as state0_ from LDU_Aquaculture.dbo.td_feeding_rates tdfeedingr0_
数据LIST - 1 的大小 12
Hibernate: select tdproductf0_.pk_fishid as pk1_7_, tdproductf0_.fishname as fishname7_, tdproductf0_.fish_store as fish3_7_, tdproductf0_.state as state7_, tdproductf0_.creationDate as creation5_7_, tdproductf0_.lastModifiedDate as lastModi6_7_ from LDU_Aquaculture.dbo.td_product_fish
tdproductf0_
数据LIST - 2 的大小 2
@@@@@@@@@@数据从数据库中全部取出!
@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!
字典MAP里的数据长度为2个
@@@@@@@@@@系统字典表数据加载成功!
系统启动,开始装载日志文件……
DEBUG日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-debug.log
INFO日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-info.log
WARN日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-warn.log
ERROR日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-error.log
2010-4-6 9:35:38 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2010-4-6 9:35:38 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2010-4-6 9:35:38 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/31 config=null
2010-4-6 9:35:38 org.apache.catalina.startup.Catalina start
信息: Server startup in 10921 ms
==========~~~~~~~~~~系统开始重新加载字典表
==========~~~~~~~~~~字典表MAP,清空成功!
%%%%%%%%%%系统启动加载......
@@@@@@@@@@开始从数据库中取数据!
Hibernate: select tdfeedingr0_.pk_id as pk1_0_, tdfeedingr0_.fishTypeId as fishTypeId0_, tdfeedingr0_.waterTemperature as waterTem3_0_, tdfeedingr0_.growthStageId as growthSt4_0_, tdfeedingr0_.baitId as baitId0_, tdfeedingr0_.feedingRate as feedingR6_0_, tdfeedingr0_.state
as state0_ from LDU_Aquaculture.dbo.td_feeding_rates tdfeedingr0_
数据LIST - 1 的大小 12
Hibernate: select tdproductf0_.pk_fishid as pk1_7_, tdproductf0_.fishname as fishname7_, tdproductf0_.fish_store as fish3_7_, tdproductf0_.state as state7_, tdproductf0_.creationDate as creation5_7_, tdproductf0_.lastModifiedDate as lastModi6_7_ from LDU_Aquaculture.dbo.td_product_fish
tdproductf0_
数据LIST - 2 的大小 2
@@@@@@@@@@数据从数据库中全部取出!
@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!
==========~~~~~~~~~~重新加载字典表成功
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/peng_wu01/archive/2010/04/06/5453361.aspx#
各位看官,请做好心理准备,这个帖子会很长,因为小弟已经做好写很长的准备。
现在说一下需求,这次的项目是做一个水产养殖相关的项目,系统开启以后很多地方要用到数据库里的字典数据,比如部门表,养殖对象表,这些数据是可变的,但是变化频率很小,所有每次用到的时候都去数据库取数据很明显的是对资源的浪费,所以想按照以前的方法把数据从数据库里读取出来,放到内存,每次用到的时候直接去内存找就可以了。想法应该是正确的,可是用到了SSH框架,做起来就有点小困难了,研究了一下午,总是是搞定。思路和代码写出来,有错误的地方请大家指正。
正式开始!中间的调错过程省去,直接如何实现。
另配一个listener,和spring同时启动,不可取。因为listener的启动机制貌似是线程,并不是按顺序一个一个启动,所有想到直接在spring的配置文件里,注册bean文件,让bean文件来执行取数据的工作,但是这个bean显然是不能使用DAO的类,因为DAO层的东西无法注入进来,所以要有个替代的东西,这个东西好难找啊,就是BeanPostProcessor接口,用类PBSTrackManagerPostProcessor实现它其中的一个方法postProcessAfterInitialization,这个方法里可以引入一个类GetDictionaryInfo,实现类的方法getAllInfo(),当getAllInfo去调用DAO层的数据时就可以了。
不说了,直接上源码:
PBSTrackManagerPostProcessor.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author ROC
* @2010年4月5日18:24:58
* 实现BeanPostProcessor接口,可以实现在Spring加载时,调取一个的对象的方法,使其能够取得实例化以后的DAO.
*
*
*
* */
public class PBSTrackManagerPostProcessor implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object obj, String s)
throws BeansException
{
if(obj instanceof GetDictionaryInfo)//GetDictionaryInfo为类名
{
((GetDictionaryInfo) obj).getAllInfo();//getAllInfo为GetDictionaryInfo的方法
}
return obj;
}
public Object postProcessBeforeInitialization(Object obj, String s)
throws BeansException
{
return obj;
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author ROC
* @2010年4月5日18:24:58
* 实现BeanPostProcessor接口,可以实现在Spring加载时,调取一个的对象的方法,使其能够取得实例化以后的DAO.
*
*
*
* */
public class PBSTrackManagerPostProcessor implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object obj, String s)
throws BeansException
{
if(obj instanceof GetDictionaryInfo)//GetDictionaryInfo为类名
{
((GetDictionaryInfo) obj).getAllInfo();//getAllInfo为GetDictionaryInfo的方法
}
return obj;
}
public Object postProcessBeforeInitialization(Object obj, String s)
throws BeansException
{
return obj;
}
}
GetDictionaryInfo.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.ArrayList;
import edu.ldu.system.preferences.feeding.DAO.TdFeedingRatesDAO;
import edu.ldu.system.preferences.product.DAO.TdProductFishDAO;
/**
* @author ROC
* 2010年4月5日18:26:55
* 实现getAllInfo方法,取得所有常用字典表里的数据
*
* */
public class GetDictionaryInfo {
@Resource
private TdFeedingRatesDAO tdFeedingRatesDAO ;
@Resource
private TdProductFishDAO tdProductFishDAO;
public GetDictionaryInfo(){
super();
}
private static WebApplicationContext springContext;
public static ServletContext _servletContext;//这个是为了取servletContext
private static GetDictionaryInfo _instance;
public static GetDictionaryInfo getInstance()
{
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
if(null == _instance)
_instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");
return _instance;
}
public static HashMap<String,ArrayList> dictionaryInfoMap = new HashMap<String, ArrayList>();
public void getAllInfo() {
System.out.println("%%%%%%%%%%系统启动加载......");
System.out.println("@@@@@@@@@@开始从数据库中取数据!");
ArrayList tdFeedingRatesList = (ArrayList)tdFeedingRatesDAO.findAll();
System.out.println("数据LIST - 1 的大小 "+tdFeedingRatesList.size());
ArrayList tdProductFishList = (ArrayList)tdProductFishDAO.findAll();
System.out.println("数据LIST - 2 的大小 "+tdProductFishList.size());
System.out.println("@@@@@@@@@@数据从数据库中全部取出!");
dictionaryInfoMap.put("tdFeedingRatesList", tdFeedingRatesList);
dictionaryInfoMap.put("tdProductFishList", tdProductFishList);
System.out.println("@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!");
}
/**
* 重新加载字典表数据!
* */
public void reloadDictionaryInfoMap()
{
System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");
dictionaryInfoMap.clear();
System.out.println("==========~~~~~~~~~~字典表MAP,清空成功!");
getAllInfo();
System.out.println("==========~~~~~~~~~~重新加载字典表成功");
}
public HashMap<String, ArrayList> getDictionaryInfoMap() {
return dictionaryInfoMap;
}
public void setDictionaryInfoMap(HashMap<String, ArrayList> dictionaryInfoMap) {
this.dictionaryInfoMap = dictionaryInfoMap;
}
}
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.ArrayList;
import edu.ldu.system.preferences.feeding.DAO.TdFeedingRatesDAO;
import edu.ldu.system.preferences.product.DAO.TdProductFishDAO;
/**
* @author ROC
* 2010年4月5日18:26:55
* 实现getAllInfo方法,取得所有常用字典表里的数据
*
* */
public class GetDictionaryInfo {
@Resource
private TdFeedingRatesDAO tdFeedingRatesDAO ;
@Resource
private TdProductFishDAO tdProductFishDAO;
public GetDictionaryInfo(){
super();
}
private static WebApplicationContext springContext;
public static ServletContext _servletContext;//这个是为了取servletContext
private static GetDictionaryInfo _instance;
public static GetDictionaryInfo getInstance()
{
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
if(null == _instance)
_instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");
return _instance;
}
public static HashMap<String,ArrayList> dictionaryInfoMap = new HashMap<String, ArrayList>();
public void getAllInfo() {
System.out.println("%%%%%%%%%%系统启动加载......");
System.out.println("@@@@@@@@@@开始从数据库中取数据!");
ArrayList tdFeedingRatesList = (ArrayList)tdFeedingRatesDAO.findAll();
System.out.println("数据LIST - 1 的大小 "+tdFeedingRatesList.size());
ArrayList tdProductFishList = (ArrayList)tdProductFishDAO.findAll();
System.out.println("数据LIST - 2 的大小 "+tdProductFishList.size());
System.out.println("@@@@@@@@@@数据从数据库中全部取出!");
dictionaryInfoMap.put("tdFeedingRatesList", tdFeedingRatesList);
dictionaryInfoMap.put("tdProductFishList", tdProductFishList);
System.out.println("@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!");
}
/**
* 重新加载字典表数据!
* */
public void reloadDictionaryInfoMap()
{
System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");
dictionaryInfoMap.clear();
System.out.println("==========~~~~~~~~~~字典表MAP,清空成功!");
getAllInfo();
System.out.println("==========~~~~~~~~~~重新加载字典表成功");
}
public HashMap<String, ArrayList> getDictionaryInfoMap() {
return dictionaryInfoMap;
}
public void setDictionaryInfoMap(HashMap<String, ArrayList> dictionaryInfoMap) {
this.dictionaryInfoMap = dictionaryInfoMap;
}
}
然后这个GetDictionaryInfo.java是个普通的类,所有要有一个servlet启动并且把取得HaspMap放到context里去
MainServlet.java
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.ArrayList;
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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MainServlet extends HttpServlet {
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
private static final long serialVersionUID = 1L;
// private SystemParamServicesImpl sysParamService;
private static WebApplicationContext springContext;
private ServletContext context;
public void init() throws ServletException {
// Put your code here
springContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
GetDictionaryInfo._servletContext=this.getServletContext();
context = this.getServletContext();
// GetDictionaryInfo getDictionaryInfo = new GetDictionaryInfo();
HashMap<String, ArrayList> dictionaryInfoMap = (HashMap<String, ArrayList>) GetDictionaryInfo.dictionaryInfoMap;
System.out.println("字典MAP里的数据长度为" + dictionaryInfoMap.size() + "个");
context.setAttribute("dictionaryInfoMap", dictionaryInfoMap);
System.out.println("@@@@@@@@@@系统字典表数据加载成功!");
}
/**
* Constructor of the object.
*/
public MainServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.ArrayList;
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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MainServlet extends HttpServlet {
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
private static final long serialVersionUID = 1L;
// private SystemParamServicesImpl sysParamService;
private static WebApplicationContext springContext;
private ServletContext context;
public void init() throws ServletException {
// Put your code here
springContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
GetDictionaryInfo._servletContext=this.getServletContext();
context = this.getServletContext();
// GetDictionaryInfo getDictionaryInfo = new GetDictionaryInfo();
HashMap<String, ArrayList> dictionaryInfoMap = (HashMap<String, ArrayList>) GetDictionaryInfo.dictionaryInfoMap;
System.out.println("字典MAP里的数据长度为" + dictionaryInfoMap.size() + "个");
context.setAttribute("dictionaryInfoMap", dictionaryInfoMap);
System.out.println("@@@@@@@@@@系统字典表数据加载成功!");
}
/**
* Constructor of the object.
*/
public MainServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
然后完成注册功能
applicationContext.XML
view plaincopy to clipboardprint?
<!-- 调用GetDictionaryInfo 读的 -->
<bean id="pbsTrackManagerPostProcess" class="edu.ldu.utils.PBSTrackManagerPostProcessor" />
<bean id="getDictionaryInfo" class="edu.ldu.utils.GetDictionaryInfo" />
<!-- 调用GetDictionaryInfo 读的 -->
<bean id="pbsTrackManagerPostProcess" class="edu.ldu.utils.PBSTrackManagerPostProcessor" />
<bean id="getDictionaryInfo" class="edu.ldu.utils.GetDictionaryInfo" />
配置web.xml让MainServlet.java自启动
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>edu.ldu.utils.MainServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>edu.ldu.utils.MainServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
然后就是如何在Action里取到数据了:
view plaincopy to clipboardprint?
ServletContext context = ServletActionContext.getServletContext();
HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
ServletContext context = ServletActionContext.getServletContext();
HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
又一个问题就是解决触发一个函数,让这些东西重新加载。
view plaincopy to clipboardprint?
GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();
GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();
reloadDictionaryInfoMap();实现起来时比较困难的,要在GetDictionaryInfo 取到ServletContext,而且要必须取到第一次实例的GetDictionaryInfo ,所有有一个东西很好用,就是Spring的context,取到他的方式
view plaincopy to clipboardprint?
private static WebApplicationContext springContext;
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
private static WebApplicationContext springContext;
springContext = WebApplicationContextUtils
.getWebApplicationContext(_servletContext);
这样就大功告成,跑起来以后的结果,列出控制台的输出数据:
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
%%%%%%%%%%系统启动加载......
@@@@@@@@@@开始从数据库中取数据!
Hibernate: select tdfeedingr0_.pk_id as pk1_0_, tdfeedingr0_.fishTypeId as fishTypeId0_, tdfeedingr0_.waterTemperature as waterTem3_0_, tdfeedingr0_.growthStageId as growthSt4_0_, tdfeedingr0_.baitId as baitId0_, tdfeedingr0_.feedingRate as feedingR6_0_, tdfeedingr0_.state
as state0_ from LDU_Aquaculture.dbo.td_feeding_rates tdfeedingr0_
数据LIST - 1 的大小 12
Hibernate: select tdproductf0_.pk_fishid as pk1_7_, tdproductf0_.fishname as fishname7_, tdproductf0_.fish_store as fish3_7_, tdproductf0_.state as state7_, tdproductf0_.creationDate as creation5_7_, tdproductf0_.lastModifiedDate as lastModi6_7_ from LDU_Aquaculture.dbo.td_product_fish
tdproductf0_
数据LIST - 2 的大小 2
@@@@@@@@@@数据从数据库中全部取出!
@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!
字典MAP里的数据长度为2个
@@@@@@@@@@系统字典表数据加载成功!
系统启动,开始装载日志文件……
DEBUG日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-debug.log
INFO日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-info.log
WARN日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-warn.log
ERROR日志文件路径为:E:/Program Files/Tomcat 6.0/webapps/Aquaculture/WEB-INF/logs/app-error.log
2010-4-6 9:35:38 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2010-4-6 9:35:38 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2010-4-6 9:35:38 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/31 config=null
2010-4-6 9:35:38 org.apache.catalina.startup.Catalina start
信息: Server startup in 10921 ms
==========~~~~~~~~~~系统开始重新加载字典表
==========~~~~~~~~~~字典表MAP,清空成功!
%%%%%%%%%%系统启动加载......
@@@@@@@@@@开始从数据库中取数据!
Hibernate: select tdfeedingr0_.pk_id as pk1_0_, tdfeedingr0_.fishTypeId as fishTypeId0_, tdfeedingr0_.waterTemperature as waterTem3_0_, tdfeedingr0_.growthStageId as growthSt4_0_, tdfeedingr0_.baitId as baitId0_, tdfeedingr0_.feedingRate as feedingR6_0_, tdfeedingr0_.state
as state0_ from LDU_Aquaculture.dbo.td_feeding_rates tdfeedingr0_
数据LIST - 1 的大小 12
Hibernate: select tdproductf0_.pk_fishid as pk1_7_, tdproductf0_.fishname as fishname7_, tdproductf0_.fish_store as fish3_7_, tdproductf0_.state as state7_, tdproductf0_.creationDate as creation5_7_, tdproductf0_.lastModifiedDate as lastModi6_7_ from LDU_Aquaculture.dbo.td_product_fish
tdproductf0_
数据LIST - 2 的大小 2
@@@@@@@@@@数据从数据库中全部取出!
@@@@@@@@@@数据打包完毕,全部放进了dictionaryInfoMap!
==========~~~~~~~~~~重新加载字典表成功
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/peng_wu01/archive/2010/04/06/5453361.aspx#
相关推荐
spring boot中文文档,从安装到部署。 I. Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot ...
taskPHP基于php开发的定时计划任务框架,利用多进程实现任务的分配和运行,利用内存共享实现进程间通信,支持多线程模式需要安装pthreads扩展(可选),支持linux和windows。有较好的伸缩性、扩展性、健壮稳定性而被多家...
在在不重启tomcat的情况下重新加载类 的一个小例子
phoenix_live_reload, 为Phoenix提供实时重新加载功能 开发过程中用于 的实时重新加载功能的项目。用法你可以在项目中使用 phoenix_live_reload,方法是将它的添加到 mix.exs 依赖项:def deps do [{:phoenix_live_...
ios开发reload
1、网上的代码: ...button class=layui-btn data-type=reload>搜索</button></div> 在js加入初始化代码和定义加载方法 layui.use('table', function(){ var table = layui.table; //方法级渲染 t
基于RELOAD协议的连接管理系统的研究与实现,解晶,双锴,P2P网络鉴于其高扩展性、自适应性和高性价比受到业界广泛关注。基于P2P网络的各种应用层出不穷。P2P-SIP是其中一大研究热点。RELOAD协��
要使用LiveReload,您需要浏览器中的客户端(此脚本)和在开发计算机上运行的服务器。 该存储库(livereload.js)实现了该协议的客户端。 客户端通过Web套接字连接到LiveReload服务器,并监听传入的更改通知。 ...
基于Python Flask框架的实现的个人博客系统 ## 主要特性 - 内置模型:栏目、标签、文章、评论等 - 数据库:使用SQLAlchemy驱动,主要支持SQLITE和Mysql两种数据库 - 编辑器:集成[editor.md] Markdown编辑器 - ...
流浪ManagedServers提供程序 ...SSH进入托管服务器。 使用任何内置的Vagrant设置程序设置托管服务器。 重新启动受管服务器。 同步文件夹支持。 通过Vagrant Share提供对托管服务器的访问。 用法
一键清理系统垃圾(Reload).bat
如果您正在开发模块,则可能会删除并重新添加它们。 此功能可以自动完成。 该功能是基于Russi Pete( 在Twitter上)而,该功能已扩展为现在还包括模块名称的自动补全功能,以使其在使用PowerShell模块时更加轻松。 ...
springboot实现数据库文档生成器 2.0原项目地址 使用springboot对原项目进行改造1. 功能介绍根据数据库表DDL生成markdown文档,支持Mysql、postgresql等数据库2. 快速使用idea打开项目, 运行项目后,在浏览器上打开...
浏览器自动刷新插件, LiveReload for Sublime text3。
【配置简单】仅需两步(绑定网络请求方法、绑定分页结果数组)轻松完成完整下拉刷新,上拉加载更多功能。 【低耦合,低侵入】分页自动管理。在page中无需处理任何分页相关逻辑,无需在data中定义任何分页相关变量,...
基于RELOAD的P2P VoIP系统中核心节点的设计与实现,姜春雨,双锴,本文研究了已有的基于P2P的VoIP系统的缺点,提出了自己的VoIP架构,重点描述了系统中核心节点SN(SuperNode)的设计和实现。SN在系统中一
利用vs code开发前端界面时,在谷歌浏览器自动刷新页面。
只需要在chrome浏览器中加载此插件然后配合vscode或其他文本编辑器的相对应插件即可实现 编辑器和浏览器之间的实时效果刷新,是不是很棒,有木有? 具体怎么使用请参考我的文章:...
FIR 高级应用 FIR Reload 在线重新载入系数的使用 https://blog.csdn.net/qq_46621272/article/details/125348908 文章有该代码详细说明 https://blog.csdn.net/qq_46621272/article/details/125292610 FIR 使用...
livereload chrome插件