【问题标题】:Is there an equivelant to spring's ContextLoader for a non-webapp对于非 webapp,是否有等效于 spring Context Loader
【发布时间】:2009-05-21 15:25:52
【问题描述】:

我想要一个类似于 spring 的 ContextLoader/ContextLoaderListener/ContextLoadServlet 的类。这些类在应用服务器初始化并将您配置的上下文放入内存时被调用。

对于没有容器包装的应用程序,这有什么类比?

这将排除多个实例化,提供统一的检索位置,并且也不会遭受双重检查锁定跛脚。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    可以在此处找到替代解决方案:

    Simple Spring, use of ClasspathApplicationContext for standalone apps, how to reuse?

    用于使用 SingletonBeanFactoryLocator。

    【讨论】:

    • 谢谢,我知道必须有一种方法让我不必担心同步和缓存。
    【解决方案2】:

    经典的是ClassPathXmlApplicationContext:

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.scripting.Messenger;
    
    public final class Boot {
    
        public static void main(final String[] args) throws Exception {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
            Messenger messenger = (Messenger) ctx.getBean("messenger");
            System.out.println(messenger);
        }
    }
    

    查看更多here

    【讨论】:

    • AbstractApplicationContext 并没有真正提供很多不是 ConfigurableApplicationContext 或 ApplicationContext 中的函数覆盖的东西。一般来说,不将 ctx 声明为完整的具体类或接口之一是没有意义的。此外,如果您只传递一个配置文件,则不需要使用字符串数组构造函数。
    • 我从文档中获取了代码 sn-p (在我的回答中提到)。 Spring 的 ApplicationContecxt 不仅仅是一个 BeanFactory——自动 BeanPostProcessor 和 BeanFactoryPostProcessor 注册,方便的 MessageSource 访问(对于 i18n)和 ApplicationEvent 发布。该文档正式声明“使用 ApplicationContext 除非您有充分的理由不这样做”。更多信息请访问static.springframework.org/spring/docs/2.5.x/reference/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2011-11-14
    相关资源
    最近更新 更多