Spring上下文层次结构 [英] Spring contexts hierarchy

查看:210
本文介绍了Spring上下文层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用一个父上下文创建多个Spring上下文. 这是我要创建父上下文的方法:

I'm going to create several Spring contexts with one parent context. Here is how I'm going to create parent context:

new ClassPathXmlApplicationContext(new String[] {"ApplicationContext/application.xml"})

我想通过以下方式创建每个父上下文:

And each parent context I want to create in following way:

PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setProperties(properties);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(appContext);
context.addBeanFactoryPostProcessor(configurer);
context.setConfigLocation("ApplicationContext/beans.xml");
context.refresh();

这个想法是在每个子上下文(DAO,服务,数据源,事务管理器等)中具有多个具有相同bean层次结构的子上下文.具有多个上下文的原因是需要具有多个不同的数据源(实际上每个应用程序上下文一个).每个数据源的数据库结构是相同的. 因此,有一些问题.

The idea is to have multiple child contexts with the same bean hierarchy in each of them (DAOs, services, data source, transaction manager, etc). The reason to have several contexts is in demand to have several different data sources (one per each application context actually). Database structure for each data source is the same. So, there are some questions.

  1. 具有这样的上下文层次结构是否安全?例如,如果有30个子上下文?
  2. 跨子上下文上下文可见性如何?说,我已经用 @Component 批注声明了 CustomerService bean,并带有几个自动装配的DAO依赖项. Spring是否在特定的子上下文中执行自动装配和其他DI动作?
  3. 此外,我将使用以下方法从子上下文中查找bean: childContext.getBean(CustomerService.class); 我是否从这个特定的子上下文而不是其他子上下文获取客户服务?我知道,春季单例是每个应用程序上下文中的单例,但仍不确定.
  1. Is it safe to have such hierarchy of contexts? For example if there are 30 child contexts?
  2. What about cross child context bean visibility? Say, I have CustomerService bean declared with @Component annotation with several autowired DAO dependencies. Does Spring perform autowiring and other DI actions within particular child context?
  3. Also, I'm going to lookup beans from child context using following method: childContext.getBean(CustomerService.class); Do I get the customer service from this specific child context and not other child context? I know, that spring singleton is a singleton per application context but still not sure.

PS. 此处中描述了另一种处理多个数据源的方法.但是对于我来说,这种方法似乎并不方便.

PS. There is another way to handle multiple data sources described here. But this approach seems to be not really convenient in my case.

推荐答案

  • 具有这样的上下文层次结构是否安全?例如,如果有30个子上下文?
  • 安全是什么意思?如果您是在初始化bean时表示线程安全,那么可以,因为上下文是一个接一个地初始化的.

    What do you mean by safety? If you mean thread safety while bean initialization then yes, since the contexts are initialized one by one.

    • 跨子上下文bean可见性如何?说,我已经用 @Component 批注声明了 CustomerService bean,并带有几个自动装配的DAO依赖项. Spring是否在特定的子上下文中执行自动装配和其他DI动作?
    • What about cross child context bean visibility? Say, I have CustomerService bean declared with @Component annotation with several autowired DAO dependencies. Does Spring perform autowiring and other DI actions within particular child context?

    Bean在子上下文中不可见.在上下文中唯一可见的bean是它自己的,并且在其父上下文中是可见的.

    Beans are not visible across child contexts. The only beans visible in a context are its own and the ones in its parent contexts.

    • 此外,我将使用以下方法从子上下文中查找bean: childContext.getBean(CustomerService.class); 是否从这个特定的子上下文而不是其他子上下文获取客户服务?我知道,春季单例是每个应用程序上下文中的单例,但仍不确定.
    • Also, I'm going to lookup beans from child context using following method: childContext.getBean(CustomerService.class); Do I get the customer service from this specific child context and not other child context? I know, that spring singleton is a singleton per application context but still not sure.

    是的.按照最后一个问题的答案.

    Yes. As per the answer to the last question.

    我在我的应用程序中相当广泛地使用了这种模式.有一个共同的上下文,许多其他子上下文通过将它们作为父上下文来共享它们.当您要在单个JVM中运行完全隔离的上下文时(例如,如果您的应用程序是多租户的),这是非常有用的.然后,您可以按租户方式启动/停止/重新启动应用程序上下文,而无需重新启动JVM.

    I use this pattern quite extensively in my applications. There is common context which is shared by many other child contexts by making it their parent. It is quite useful when you want to run completely isolated contexts inside a single JVM, for example if you application is a mutli-tenant one. Then you can start/stop/restart application contexts tenant-wise without restarting the JVM.

    这还可以将数据源和事务管理器明确分开,并允许人们轻松地分拆其数据库.

    This also allows a clear separation of data sources and transaction managers and allows one to shard their databases easily.

    这篇关于Spring上下文层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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