本文共 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/