【问题标题】:load the value from database when the server is started服务器启动时从数据库中加载值
【发布时间】:2016-10-10 18:23:35
【问题描述】:

我正在开发基于 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

以前有没有人遇到过这种情况。当服务器启动时,连接到数据库并从数据库中获取值并存储在缓存中的最佳方法是什么?建议会很有帮助。

--已编辑--

我在服务器启动时尝试使用@PostConstruct 来调用 loadData() 的方法。但是当我签入日志文件时,打印语句不存在,这表明在服务器启动时没有调用该方法开始了。

@Component
public class MyDataStore {
    @PostConstruct
    public void loadData()
    {
        System.out.println("--In @postconstruct, loadData-");
    }
}

【问题讨论】:

    标签: java spring servlets jboss ehcache


    【解决方案1】:

    不要使用 servlet 的东西,只需在 bean 中使用注释为 @PostConstruct 的方法做你想做的事,但这不一定是第一个创建的 bean。如果您需要在 spring 本身启动之前做任何事情,您需要手动在 main 方法中创建应用程序上下文。您希望在春季开始之前发生的任何事情都需要在创建应用程序上下文之前发生。如果您希望在其他 bean 之前创建一些 bean,则它需要成为所有其他 bean 的依赖项,这真的很烦人,不知道有任何其他方法可以做到这一点。

    【讨论】:

    • 我尝试使用 PostConstruct,但是当我启动服务器时,没有调用带有 PostConstruct 的方法。我是否需要进行任何其他配置才能使带有带注释的 PostConstruct 的方法工作?
    • 不,但请确保正在创建 bean。在构造函数中放置一个 System.out.println 或其他内容以确保是这种情况。
    • 我已经使用带有 System.out.print 语句的注释方法 PostConstruct 创建了类,并且在服务器启动时没有被调用。
    • 但是 bean 本身是被创建的吗?
    • 没有。请看我上面编辑过的帖子。我已经用我在我的应用程序中尝试过的带注释的 PostConstruct 方法添加了这个类。我错过了什么吗?
    【解决方案2】:

    您的问题不清楚为什么您需要在ServletContextListener 中执行缓存逻辑,但我可以建议如何在其中访问您的applicationContext

    您可以使用缓存逻辑扩展org.springframework.web.context.ContextLoaderListener,并使用它来加载应用程序上下文。此外,如果您需要 HibernateDaoSupport 逻辑,则可以使用组合而不是继承来添加它。

    所以所有这些可能如下所示:

    MyInitializationListener

    public class MyInitializationListener extends ContextLoaderListener {
    
        private static CacheManager singletonManager = null;
    
        pirvate HibernateDaoSupport hibernateDaoSupport;
    
        ......
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            super.contextInitialized(event);
    
            ApplicationContext applicationContext = getCurrentWebApplicationContext();
    
            hibernateDaoSupport = applicationContext.getBeansOfType(HibernateDaoSupport.class).values().iterator().next();
    
            //Do cahcing logic you need;
        }
    
        ......
    
    }
    

    web.xml

    <!-- spring framework context configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>MyInitializationListener</listener-class>
    </listener>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多