是否可以在不使用ApplicationContextAware的情况下检索具有原型范围的Spring bean [英] Is it possible to retrieve a Spring bean with prototype scope without using ApplicationContextAware

查看:83
本文介绍了是否可以在不使用ApplicationContextAware的情况下检索具有原型范围的Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring 3.1。如果我想检索具有原型范围的bean(即每次都想要一个不同的类实例),是否可以在不必使用ApplicationContextaware类的情况下检索bean?

Using Spring 3.1. If I want to retrieve a bean with prototype scope (i.e. I want a different instance of the class each time), is it possible to retrieve the bean without having to use an ApplicationContextaware class?

这是我目前的操作方式

@Component
@Qualifier("MyService")
public class MyServiceImpl implements MyService {

    @Override
    public void doSomething() {
        Blah blah = (Blah)ApplicationContextProvider.getContext().getBean("blah");
        blah.setThing("thing");
        blah.doSomething();
    }
}


@Component("blah")
@Scope("prototype")
public class Blah {
    ....
}

其中ApplicationContextProvider实现了ApplicationContextAware。

where ApplicationContextProvider implements ApplicationContextAware.

是否可以通过注释或简单的Spring配置来执行此操作而不必使用ApplicationContextAware类?

Is it possible to do this with annotations or simple Spring configuration without having to use an ApplicationContextAware class?

推荐答案

Spring有一些相当成熟的方法来实现您的追求...

Spring has some fairly sophosticated methods for achieving what you're after...

请参见spring文档: http://static.springsource。 org / spring / docs / 3.1.x / spring-framework-reference / html / beans.html#beans-factory-scopes-other-injection

See the spring documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection

在Google上搜索Spring代理范围也引发了一些结果...

Searching for spring proxy scope on google also threw up some results...

这篇关于是否可以在不使用ApplicationContextAware的情况下检索具有原型范围的Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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