如何在 Spring 的 applicationContext.xml 中指定默认范围来请求范围? [英] How to specify the default scope in Spring's applicationContext.xml to request scope?

查看:61
本文介绍了如何在 Spring 的 applicationContext.xml 中指定默认范围来请求范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让所有 bean 请求默认范围内,但是 Spring 文档说默认范围是单例.(第 3.4.1 和 3.4.2 节 http://static.springsource.org/spring/docs/2.5.x/reference/beans.html)

I want to make all beans request scoped by default, however the Spring documentation says that the default scope is Singleton. (sections 3.4.1 and 3.4.2 http://static.springsource.org/spring/docs/2.5.x/reference/beans.html)

我想将默认范围声明为请求范围.

I want to declare the default scope to be request scoped.

这是迄今为止我发现的最接近的东西——这是一个已经有一段时间没有被触及的缺陷.jira.springframework.org/browse/SPR-4994?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs

This is the closest thing I have found so far -- it's a defect that has not been touched in some time. jira.springframework.org/browse/SPR-4994?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs

推荐答案

spring-beans.xsd 文件中没有定义 default-scope 属性.但根据 BeanDefinition API

There is no default-scope Attribute defined in spring-beans.xsd file. But according To BeanDefinition API

扩展的 bean 工厂可能支持更多的范围.

WebApplicationContext - 扩展的 ApplicationContext 支持 request 范围

And WebApplicationContext - A extended ApplicationContext supports request scope

除了标准范围singleton"和prototype"外还支持

Supported in addition to the standard scopes "singleton" and "prototype"

所以当你有一个 WebApplicationContext 时使用请求范围是有意义的.如果要将 WebApplicationContext 中定义的所有 bean 注册为请求范围,您必须定义一个 BeanFactoryPostProcessor

So just make sense To use request scope when you have a WebApplicationContext. And if you want To register of all beans defined in WebApplicationContext as request scoped, you must define a BeanFactoryPostProcessor

public class RequestScopedPostProcessor implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
        for(String beanName: factory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);

            beanDefinition.setScope("request");
        }
    }
}

不要忘记注册你的 BeanFactoryPostProcessor

And do not forget register your BeanFactoryPostProcessor

<bean class="RequestScopedPostProcessor"/>

但请记住

此方法不考虑祖先工厂.它仅用于访问此工厂的本地 bean 定义

This method does not consider ancestor factories. It is only meant for accessing local bean definitions of this factory

所以上面定义的 BeanFactoryPostProcessor 只是覆盖了 scope 属性,无论你的 bean 是在你的 WebApplicationContext 中定义的

So The BeanFactoryPostProcessor defined above just overrides scope property whether your bean is defined in your WebApplicationContext

更新

有没有办法将一些默认的请求"范围的bean覆盖为单例范围?

您应该再次使用上面提供的相同 BeanFactoryPostProcessor.我不确定,但我认为设置其范围的唯一方法是使用 beanDefinition.setScope 方法.并且有很多有用的方法可以检索有关任何 bean 的信息.参见 ConfigurableListableBeanFactory

Again you should use The same BeanFactoryPostProcessor provided above. I am not sure but I Think The only way you can set up its scope is by using beanDefinition.setScope method. And There is a lot of useful methods you can retrieve information about any bean. See ConfigurableListableBeanFactory such as

  • getBeanNamesForType

...

/**
  * Suppose Service is an interface
  *
  * And you want to define all of Service implementations as singleton
  */
String [] beanNameArray = factory.getBeanNamesForType(Service.class);
for(String beanName: beanNameArray) {
    BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);

    beanDefinition.setScope("singleton");
}

希望有用

这篇关于如何在 Spring 的 applicationContext.xml 中指定默认范围来请求范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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