简体   繁体   English

Spring的Javaconfig和原型Bean

[英]Spring's Javaconfig and Prototyped Beans

I've moved my code from Spring's XML configuration to Java Configuration. 我已经将代码从Spring的XML配置迁移到Java配置。 I have everything working, but I have a question about how I implemented prototype beans - mainly, while what I'm doing works, is it the best way to do this? 我一切正常,但是我有一个关于如何实现原型bean的问题-主要是,当我在做什么的时候,这是最好的方法吗? Somehow it just feels off! 不知何故它感觉不对!

I wrote the bean class this way: 我这样写了bean类:

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

Then to use the bean - this is the part that I'm just not sure about, although it does work: 然后使用Bean-尽管确实有效,但这是我不确定的部分:

@Component
public class BeanUser implements ApplicationContextAware {
    ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context)throws BeansException
    {
        this.context = context;
    }

    public void getProtoBean() {
         ProtoBean protoBean = context.getBean(ProtoBean.class);
    }
}

This gets me a prototyped bean, and in unit tests I just mocked the context, called setApplicationContext with the mock, and had the getBean call of the mock return a mock ProtoBean. 这为我提供了一个原型bean,并且在单元测试中,我只是模拟了该上下文,并使用模拟程序调用了setApplicationContext,并使模拟程序的getBean调用返回了模拟程序ProtoBean。 So all is well. 一切都很好。

I did this in the XML by using a factory, but that didn't seem to work too well, so this is where I ended up. 我通过使用工厂在XML中进行了此操作,但效果似乎不太好,所以这就是我要结束的地方。 But is there a way to do this without the context? 但是,没有上下文,有没有办法做到这一点? Or just a better way? 还是更好的方法?

Thanks! 谢谢!

I don't think is so much an issue of Spring XML vs Java-base configuration, but one of matching dependency scopes. 我不认为Spring XML与基于Java的配置有太大关系,而是匹配的依赖范围之一。 Since Spring can only do dependency injection on the singleton-scoped bean at creation time, you have to lookup the prototype-scoped bean on demand. 由于Spring在创建时只能对单例作用域的bean进行依赖项注入,因此您必须按需查找原型作用域的bean。 Of course the current bean-lookup approach works, but creates a dependency on the ApplicationContext. 当然,当前的bean查找方法有效,但是会创建对ApplicationContext的依赖。 I can suggest a few other possibilities but the root of the issue is really what is involved in producing a ProtoBean, and what trade-offs should you accept. 我可以提出其他几种可能性,但问题的根源实际上是生产ProtoBean涉及的是什么,以及您应该接受哪些折衷。

You could make BeanUser itself prototype-scoped, which would allow you to wire in the ProtoBean as a member. 您可以使BeanUser本身具有原型作用域,这将使您可以将ProtoBean作为成员加入。 Of course the trade-off is you now have the same problem on the clients of BeanUser, but sometimes that would not be a problem. 当然,需要权衡的是您现在在BeanUser的客户端上遇到了相同的问题,但是有时这不会成为问题。

Another path could be using something like a singleton-scoped ProtoBeanFactory to provide ProtoBean instances, and hiding dependency lookups within the ProtoBeanFactory. 另一条路径可能是使用类似单例作用域的ProtoBeanFactory来提供ProtoBean实例,并在ProtoBeanFactory中隐藏依赖项查找。

Finally, you could use a scoped-proxy bean to effectively hide the factory. 最后,您可以使用带作用域的代理bean有效地隐藏工厂。 It uses AOP to do this, and isn't always clear to others what sort of voodoo you have going. 它使用AOP来做到这一点,并且并不总是向他人清楚您将使用哪种伏都教。 With XML you'd use <aop:scoped-proxy/> on the bean declaration. 对于XML,您可以在bean声明中使用<aop:scoped-proxy/> For annotations you'd use: 对于注释,您将使用:

@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring JavaConfig 相当于按名称引用 bean - Spring JavaConfig equivalent of referring to beans by name JavaConfig Spring使Bean可用于所有应用程序 - JavaConfig Spring make beans available to all application 使用Spring JavaConfig的bean的初始化映射 - Initialization map of beans using Spring JavaConfig Spring的JavaConfig和CustomScopeConfigurer问题 - Spring's JavaConfig and CustomScopeConfigurer issue 春天的简单示例:在JavaConfig中的何处创建bean列表? - Spring simple example: where create list of beans in JavaConfig? Spring JavaConfig:通过“直接”调用Configuration类/ bean来检索bean - Spring JavaConfig: Retrieving beans by calling Configuration class/bean “directly” 春季:如何在一个弹簧容器中处理原型豆,该容器的对象范围是对象图的某个子部分? - Spring: How to treat prototyped beans in a spring container, singleton scoped for a certain subpart of the object graph? Spring JavaConfig我如何引用为创建新bean而定义的bean - Spring JavaConfig how do I refer to beans that I have defined to create new beans 独立的Java应用程序,在Spring Application Context中没有创建bean吗? (IntelliJ,JavaConfig) - Standalone Java Application, no beans get created in the Spring Application Context? (IntelliJ, JavaConfig) JavaConfig中的Spring Bean别名 - Spring Bean Alias in JavaConfig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM