java 如何将 bean 加载到 Spring MVC 的应用程序上下文中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:33:42  来源:igfitidea点击:

How do you load a bean into Spring MVC's application context?

javaspringspring-mvc

提问by James McMahon

As I understand it, Spring MVC application has two distinct contexts, the application context and the web context, which are controlled by applicationContext.xml and dispatcher-servlet.xml, respectively.

据我了解,Spring MVC 应用程序有两个不同的上下文,应用程序上下文和 Web 上下文,分别由 applicationContext.xml 和 dispatcher-servlet.xml 控制。

Inside my controllers, how do I go about loading a bean into either of these contexts?

在我的控制器中,我如何将 bean 加载到这些上下文中?

Note that I am aware of Getting Spring Application Context. That would answer my question for a stand alone application. Where I would use a factory to load the application context from the xml file, but this seems like the wrong way to go about loading beans in Spring MVC.

请注意,我知道Getting Spring Application Context。这将回答我对独立应用程序的问题。我将使用工厂从 xml 文件加载应用程序上下文,但这似乎是在 Spring MVC 中加载 bean 的错误方法。

回答by Dónal Boyle

Although a Spring MVC application has two distinct contexts, the web context has access to all the beans loaded in the application context. The application context however cannot access beans in the web context. This is used to enforce separation of concerns, e.g. business rules class does not need to know about the HTTP session. So if you have a bean you need access to from both contexts it will have to be declared within the application context.

尽管 Spring MVC 应用程序有两个不同的上下文,但 Web 上下文可以访问应用程序上下文中加载的所有 bean。但是,应用程序上下文无法访问 Web 上下文中的 bean。这用于强制分离关注点,例如业务规则类不需要知道 HTTP 会话。因此,如果您有一个需要从两个上下文访问的 bean,则必须在应用程序上下文中声明它。

回答by Juri

Mattis absolutely correct. You should not need with any kind of bean-loading/instantiating code in your MVC application, otherwise you're doing something wrong. You define your beans inside the according spring XML configuration files.

马特是完全正确的。您不应该在 MVC 应用程序中使用任何类型的 bean 加载/实例化代码,否则您做错了什么。您可以在相应的 spring XML 配置文件中定义 bean。

<bean id="pinboardServiceTarget" class="com.lifepin.services.PinboardService">
    <property name="pinboardEntryDao" ref="pinboardEntryDAO"/>
</bean>
...
<bean id="pinboardEntryDAO" class="com.lifepin.daos.PinboardEntryDAO">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

Your PinboardService class (this is just from an application I wrote recently) will have a property IPinboardEntryDAOlike

您的 PinboardService 类(这只是来自我最近编写的应用程序)将具有IPinboardEntryDAO类似的属性

public class PinboardService implements IPinboardService{
  private IPinboardEntryDAO pinboardEntryDao;

  ...

  public void setPinboardEntryDAO(IPinboardEntryDAO dao){
     this.pinboardEntryDao = dao;
  }

  public IPinboardEntryDAO getPinboardEntryDAO(){
    ...
  }

  ...
}

public class PinboardEntryDAO implements IPinboardEntryDAO{
   ...
}

Note that inside the the PinboardServiceclass I'm using the DAO interface, not the implementation itself, while in the configuration I'm then injecting the real implementation PinboardEntryDAO. This is a very good practice for separating the different layers (presentation, service and data layer).

请注意,在PinboardService类内部,我使用的是 DAO 接口,而不是实现本身,而在配置中,我将注入真正的实现PinboardEntryDAO。这是分离不同层(表示层、服务层和数据层)的一个很好的做法。

回答by suresh

In stand alone application we can user context.Refresh() it will reloading/re-instantiating the new requested beans the old beans will have the old instance only.

在独立应用程序中,我们可以使用 context.Refresh() 它将重新加载/重新实例化新请求的 bean,旧 bean 将仅具有旧实例。

In web applications we need to overwrite the ContextLoaderListener and call the contextInitialized()

在 Web 应用程序中,我们需要覆盖 ContextLoaderListener 并调用 contextInitialized()

回答by amyzhou

You need to import the file containing the bean definitions of the service layer(say, service-context.xml) into the new project. It can be done as:

您需要将包含服务层 bean 定义的文件(比如 service-context.xml)导入到新项目中。可以这样做:

<import  resource="classpath:service-context.xml"/>

回答by matt b

Any dependencies that your Controller has (such as on service-layer classes, DAOs, etc) should be expressed as normal - through injection, either constructor injection or setter injection.

您的控制器具有的任何依赖项(例如服务层类、DAO 等)都应表示为正常 - 通过注入,构造函数注入或 setter 注入。

The context where the controller is mapped just wires it up with any dependencies it needs as normal. The Controller code never needs to work with Spring directly to get any beans, it is wired up with them.

控制器映射的上下文只是将它与正常需要的任何依赖项连接起来。控制器代码永远不需要直接使用 Spring 来获取任何 bean,它与它们相连。

回答by Rafe

You should use dependency injection and your config files to load beans into your controllers, but if you do need to access the application context directly, any Controller that extends AbstractController (or any of its descendents) has access to the getApplicationContext()method.

您应该使用依赖注入和您的配置文件将 bean 加载到您的控制器中,但是如果您确实需要直接访问应用程序上下文,任何扩展 AbstractController(或其任何后代)的控制器都可以访问该getApplicationContext()方法。