我正在研究基于spring和servlet的应用程序。 我的要求是从数据库加载数据并存储在ehcache中。我写了一个监听器,一旦jboss服务器启动就调用它。但问题出在我的应用程序中,我们使用spring hibernate类来连接数据库并从数据库加载数据。 根据要求,数据必须从数据库中检索并存储在缓存对象(ehcache)中。但是,当侦听器类被加载时,其他XML配置文件(applicationContext.xml ..)尚未加载到配置数据源详细信息以连接到数据库的位置。以下是我的代码:服务器启动时从数据库加载值
监听器类。
import net.sf.ehcache.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/imports
public class MyInitializationListener extends HibernateDaoSupport implements ServletContextListener {
/** Singleton instance of CacheManager. */
private static CacheManager singletonManager = null;
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("--ServletContextListener destroyed*--");
}
private static CacheManager getInstance() {
if (singletonManager == null) {
singletonManager = CacheManager.create();
}
return singletonManager;
}
private Cache getCache() {
Cache cache = null;
cache = MyInitializationListener.getInstance().getCache("myCache");
return cache;
}
// Run this before web application is started
public void contextInitialized(ServletContextEvent arg0) {
final Cache cache = getCache();
final String dbValue = getDBValue();
//logic here
}
public String getDBValue() throws DataLayerException{
//logic to get the value from database
}
以下是例外:
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/eas-webservice/ivr]] Exception sending context initialized event to listener instance of class com.data.listener.MyInitializationListener
java.lang.NullPointerException
没有任何一个面对这种情况before.What将是最好的方式连接到数据库,并在得到缓存从数据库和存储的值服务器启动。建议会有帮助。
--EDITED--
我试着使用@PostConstruct的方法来调用loadData()当服务器started.But当我在日志文件中打印语句检查以下方式是不存在其中规定,该方法在服务器启动时未被调用。
@Component
public class MyDataStore {
@PostConstruct
public void loadData()
{
System.out.println("--In @postconstruct, loadData-");
}
}
我试着用PostConstruct,但是当我开始在服务器没有调用PostConstruct的方法。我是否需要进行其他配置以使PostConstruct的工作方式变为可用? – javaUser
不能,但确保bean正在创建。将System.out.println或其他东西放在构造函数中以确保是这种情况。 – Snickers3192
我已经使用带有System.out.print语句的注释方法PostConstruct创建了该类,并且在服务器启动时未调用该类。 – javaUser