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

2024-03-15

使用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 {
    ....
}

其中ApplicationContextProvider实现ApplicationContextAware。

是否可以通过注释或简单的 Spring 配置来完成此操作,而无需使用 ApplicationContextAware 类?


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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐