是否可以在不使用ApplicationContextAware的情况下检索具有原型范围的Spring bean

Cod*_*ber 5 spring dependency-injection

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

这就是我目前的做法

@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 {
    ....
}
Run Code Online (Sandbox Code Playgroud)

ApplicationContextProvider实现ApplicationContextAware的位置.

是否可以使用注释或简单的Spring配置执行此操作而无需使用ApplicationContextAware类?

Mic*_*les 1

Spring 有一些相当复杂的方法来实现你所追求的......

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

在谷歌上搜索 spring 代理范围也给出了一些结果......

  • 是的,我想这就是我正在寻找的东西。这个问题展示了如何使用注释来做到这一点:http://stackoverflow.com/questions/4503606/annotation-equivalent-of-aopscoped-proxy (2认同)