web应用程序在启动时从数据库初始化

web应用程序在启动时从数据库初始化

本文介绍了spring web应用程序在启动时从数据库初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3.1 + Tomcat

Spring 3.1 + Tomcat

我在这里有一些设计问题:

I have a bit of a design question here:

有一个已在数据库中指定的类别组。这些类别可以被认为是全局的,因为它们可以在整个webapp中使用。我想要做的是在服务器启动时阅读这些类别,并在Java中填充某些类型的集合。只需要在启动时从数据库读取一次,将其视为一种初始化。

There are a group of categories that have been specified in a database. These categories can be considered global in the sense that they could be used throughout the webapp. What I would like to do is read these categories on server startup and populate some type of collection in Java. The only need to be read from the database once at startup, consider it a type of initialization.

我能想到的两个选项:

1)我应该使用非懒惰的初始化bean吗?

1) Should I use a NON lazily initialized bean?

2 )修改web.xml?

2) Modifying the web.xml?

我不确定什么是首选方法,并且非常感谢任何有关如何执行推荐的说明。谢谢!

I'm not really sure what is the preferred method, and any instructions on how to perform your recommended would be much appreciated. Thanks!

推荐答案

您提供的选项最常用:


  1. 使用带有 @PostConstruct 注释的方法的singleton非懒惰bean(但请注意 @Transactional )。你可以有几个带有这种初始化例程的bean。

  1. Use singleton non-lazy bean with a method annotated with @PostConstruct (but be aware that @Transactional might not work). You can have several beans with such initialization routine.

扩展 org.springframework.web.context.ContextLoaderListener 并在 web.xml 中使用它。我发现这个解决方案不那么优雅,并且还促进了糟糕的编程风格(通过调用 super 扩展以增强基类)

Extend org.springframework.web.context.ContextLoaderListener and use it in web.xml. I find this solution less elegant and also promoting bad programming style (extending with call to super to enhance the base class)

这篇关于spring web应用程序在启动时从数据库初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:30