博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSH开发框架中,实现系统启动加载类,读取数据库常用数据进入内存以及将数据放在application
阅读量:4036 次
发布时间:2019-05-24

本文共 3064 字,大约阅读时间需要 10 分钟。

如果想让bean类在初始化时启动某个方法,在xml里设置<bean id="test" class="xxx.xxx.Test" init-method="init"></bean> 或者使用annotation在方法上配置@PostConstruct即可。

但如果bean类设置为scope="prototype"@Scope("prototype") 那该bean类只有在调用是才被初始化,那init方法也只能在改bean被调用是才执行,因为设置了prototype的原因每次请求都会创建一个新的bean类,所以每次都会执行一次该init方法。(default-lazy-init="true" 的情况还没试过)

如果bean类设置为scope="singleton"@Scope("singleton") ,那么bean类会在服务器启动时被初始化,同时方法也执行init方法。(default-lazy-init="true" 的情况还没试过)

 

个人所遇到的问题以及解决方案

问题一:

bean类中注入dao类,并在init方法中调用操纵数据库,接过启动过程报java.lang.NullPointerException异常,后来搜索了一下,搜到以下内容:

“另配一个listener,和spring同时启动,不可取。因为listener的启动机制貌似是线程,并不是按顺序一个一个启动,所有想到直接在spring的配置文件里,注册bean文件,让bean文件来执行取数据的工作,但是这个bean显然是不能使用DAO的类,因为DAO层的东西无法注入进来,所以要有个替代的东西,这个东西好难找啊,就是BeanPostProcessor接口,用类PBSTrackManagerPostProcessor实现它其中的一个方法postProcessAfterInitialization,这个方法里可以引入一个类GetDictionaryInfo,实现类的方法getAllInfo(),当getAllInfo去调用DAO层的数据时就可以了。”

参考地址:

参考了一下

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;

@Service

public class InitPostProcessor implements BeanPostProcessor {

 public Object postProcessAfterInitialization(Object obj, String s)

   throws BeansException {
  if(obj instanceof InitAction)//InitAction为类名   
        {  
            ((InitAction) obj).init();//init为InitAction的方法   
        }  
        return obj; 
 }

 public Object postProcessBeforeInitialization(Object obj, String s)

   throws BeansException {
  return obj;
 }

}

在InitAction类中init方法调用到即可解决

 

问题二:

基于问题一,如果想将init方法里加载出来的数据保存到application(或者说保存到getServletContext中),发现问题又来了,同样也在启动过程报java.lang.NullPointerException异常,后来找了很多解决方案也没有解决,最后只能用最直接的方法,新建一个servlet,第一个InitPostProcessor可以不用了

其实还可以参考一下刚才地址的做法

参考地址:

 

import java.util.List;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public class InitServlet extends HttpServlet{

 private static WebApplicationContext springContext;  
    private ServletContext context;
    
    
 @Override
 public void init() throws ServletException {
  super.init();
  springContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); 
  InitAction._servletContext = this.getServletContext();  
  context = this.getServletContext(); 
  InitAction.getInstance().init();
  List<String> list = InitAction.list;
  context.setAttribute("list", list);
 } 
}

 

public class InitAction extends BaseAction{

    private static WebApplicationContext springContext;  

    public static ServletContext _servletContext;
    private static InitAction _instance;
    public static List<String> list;
 
 
    public static InitAction getInstance()  {  
       springContext = WebApplicationContextUtils.getWebApplicationContext(_servletContext); 
        if(null == _instance) _instance = (InitAction)springContext.getBean("initAction"); 
       return _instance;  
    }

 

最后:在web.xml里添加

    <servlet>  

        <servlet-name>InitServlet</servlet-name>  
        <servlet-class>xxx.xxx.InitServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>

不需要添加  </servlet-mapping>

转载地址:http://cfjdi.baihongyu.com/

你可能感兴趣的文章
S3C2440 USB 设备控制器(转)
查看>>
Linux usb 设备驱动 (1)
查看>>
解决跨网场景下,CAS重定向无法登录的问题(无需修改现有代码)
查看>>
java反编译命令
查看>>
activemq依赖包获取
查看>>
概念区别
查看>>
关于静态块、静态属性、构造块、构造方法的执行顺序
查看>>
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
JMeter 保持sessionId
查看>>
IDEA Properties中文unicode转码问题
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>