在独立应用程序中使用Spring [英] Using Spring in standalone apps

查看:189
本文介绍了在独立应用程序中使用Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring,我对如何在独立应用程序中使用它(以及在使用它来制作Web应用程序时)提出了一个问题。到目前为止我编写的示例使用了一个简单的main方法,我通过调用getBean来检索bean。上下文对象。但是,您可能希望在多个类中执行此操作,因此您首先获取上下文然后调用getBean还是有其他更清洁的替代方案?或者这是你在独立和网络应用程序中这样做的方式吗?

I am learning Spring and I have a question regarding how you use it in standalone applications (and also when using it for making web applications). The examples I have been coded so far has used a simple main method where I retrieve beans by calling getBean on the. Context object. However, you probably want to do this in multiple classes so do you first get a context and then call getBean or are there other cleaner alternatives? Or is this the way you do it in standalone and web apps?

推荐答案

如果你正在调用 context.getBean()无处不在,你可能错过了Spring的全部内容,这是一个依赖注入框架。

If you're calling context.getBean() everywhere, you're probably missing the whole point of Spring, which is a dependency injection framework.

独立app,你通常只调用 context.getBean()一次(或者至少很少),以获得一个rootbean。这个bean由Spring注入其他bean,依此类推。

In a standalone app, you typically call context.getBean() only once (or at least, very rarely), in order to get a "root" bean. This bean is injected by Spring with other beans, and so on.

在Web应用程序中,这完全取决于您使用的框架。但通常情况下,您在web.xml中注册一个监听器,为您加载上下文,并由Spring创建和/或注入控制器。

In a web app, it all depends on which framework you use. But typically, you register a listener in the web.xml which loads the context for you, and controllers are created and/or injected by Spring.

这篇关于在独立应用程序中使用Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆