【问题标题】:Initializing Application scoped bean in Spring MVC在 Spring MVC 中初始化应用程序范围的 bean
【发布时间】:2013-02-26 17:09:54
【问题描述】:

我想为应用程序中的所有下拉菜单初始化一个 bean 或 ArrayList,例如键值对列表,这些下拉菜单可以在应用程序范围内的会话中使用。并且希望在应用程序启动期间发生。我尝试实现 ServletContextListener 并将 bean 添加到上下文中,但它不起作用。

关于如何实现这一点的任何建议。谢谢。

拉维

【问题讨论】:

    标签: spring web-applications spring-mvc


    【解决方案1】:

    定义一个常规(单例)Spring bean 并在构造函数或@PostConstruct 方法中初始化您的值:

    import javax.annotation.PostConstruct;
    @Component
    public class AppBean {
    
    @PostConstruct
    protected void init() {
        // executed after dependencies have been injected. initialize values here
    }
    
    }
    

    查看http://www.mkyong.com/spring/spring-postconstruct-and-predestroy-example/了解更多详细示例

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 2013-08-10
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2020-11-30
      • 2011-01-23
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多