论坛首页 Java企业应用论坛

如何访问多个web应用下的配置文件?

浏览 6820 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-03-16  
我现在有三个web应用 weba, webb, webc. 它们各自都存在部分的spring容器配置文件, 而且配置文件之间有互相依赖的关系.

我现在发现通过<property name="xxx"><ref bean="xxx"></property>
的方法是无法跨越容器访问的. 请问这样的需求该如何实现?
   发表时间:2005-03-16  
自己实现ContextLoaderListener,ContextLoader
0 请登录后投票
   发表时间:2005-03-16  
scud 写道
自己实现ContextLoaderListener,ContextLoader


please for example?
0 请登录后投票
   发表时间:2005-03-16  
先看一下ContextLoader的javadoc,可以了解到有几个参数是可以配置的,就可以实现把缺省的XmlWebApplicationContext换成FileSystemXmlApplicationContext或者ClassPathXmlApplicationContext

应该就可以了吧

随便还可以了解一下 SingletonBeanFactoryLocator,不过可能用不上
0 请登录后投票
   发表时间:2005-03-16  
scud 写道
先看一下ContextLoader的javadoc,可以了解到有几个参数是可以配置的,就可以实现把缺省的XmlWebApplicationContext换成FileSystemXmlApplicationContext或者ClassPathXmlApplicationContext

应该就可以了吧

随便还可以了解一下 SingletonBeanFactoryLocator,不过可能用不上


你的回答似乎是让我在装载ApplicationContext.xml的时候去装载非默认位置的配置文件?

可能提问和回答有一些偏差, 我来明确一下, 现在我的系统里存在三个ServletContext,每个里面都有一个spring container.

如果我想三个不同的Context下的bean互相引用,是否可行? 如果可行该怎么做?

我在spring的reference中似乎只发现它提到了一个容器多个配置文件, 但没有提到多个容器下该如何解决.
0 请登录后投票
   发表时间:2005-03-16  
引用
如果我想三个不同的Context下的bean互相引用,是否可行? 如果可行该怎么做?


我想是不可以的。
理由:不同的Web App的java类是由不同的Web App Class Loader载入的,所以无法互相引用
0 请登录后投票
   发表时间:2005-03-16  
可以的,使用SingletonBeanFactoryLocator,
在项目中这样做的。sping有这样的例子,仔细找找。
0 请登录后投票
   发表时间:2005-03-17  
nihongye 写道
可以的,使用SingletonBeanFactoryLocator,
在项目中这样做的。sping有这样的例子,仔细找找。


看了一下SingletionBeanFactoryLocator, 我的理解是, 在一个Web App中我们可以将ApplicationContext分成很多"层", 这样做能够使得结构更清晰, 而且可以导入lazy-init, 使得每一个Context只有在用到的时候才加载, 保证了一个Web App下的第三方代码插入时的扩展性

但我还是没有发现多个Web App的解决方案, 比如我现在有三个ContextPath
/defaultroot, /framework, /workflow 它们每个都是一个Web App的起始路径, 我在一个Web App下的ApplicationContext.xml 中无法描述其他Web App下ApplicationContext.xml的位置.它们已经不是层次之间的关系了.

也许我的理解有误, 如果你有解决方案的话, 还请show一下代码.
0 请登录后投票
   发表时间:2005-03-17  
贴Spring reference上的一段话,但是没有说具体怎么做。我主要是不明白SingletionBeanFactoryLocator会由谁来载入。由Web app ClassLoader是不合适的,难道由EJB classLoader载入么?如果是独立的几个Web app ClassLoader,没有打成EAR包的呢?或者说在Tomcat这种servlet 容器中怎么办?
引用
As another example, in a complex J2EE apps with multiple layers (i.e. various JAR files, EJBs, and WAR files
packaged as an EAR), with each layer having its own ApplicationContext definition (effectively forming a
hierarchy), the preferred approach when there is only one web-app (WAR) in the top hierarchy is to simply
create one composite ApplicationContext from the multiple XML definition files from each layer. All the
ApplicationContext variants may be constructed from multiple definition files in this fashion. However, if there
are multiple sibling web-apps at the top of the hierarchy, it is problematic to create an ApplicationContext for each web-app which consists of mostly identical bean definitions from lower layers
, as there may be issues due
to increased memory usage, issues with creating mutliple copies of beans which take a long time to initialize
(i.e. a Hibernate SessionFactory), and possible issues due to side-effects. As an alternative, classes such as
ContextSingletonBeanFactoryLocator [???] or SingletonBeanFactoryLocator
[http://www.springframework.org/docs/api/org/springframework/beans/factory/access/SingletonBeanFactoryLocator.may be used to demand load multiple hierarchical (i.e. one is a parent of another) BeanFactories or
ApplicationContexts in an effectively singleton fashion, which may then be used as the parents of the web-app
ApplicationContexts. The result is that bean definitions for lower layers are loaded only as needed, and loaded
only once.
0 请登录后投票
   发表时间:2005-03-17  
1.1.4的spring开始支持这种加载方式了,具体可以看下ContextLoader或者
http://opensource.atlassian.com/projects/spring/browse/SPR-719
:
beanreffactory.xml的例子,该文件放在web应用的上一层classloader控制的目录下。
在tomcat下可以是common或者sharelib,使用ear方式则在根目录下

<beans>
	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
	
	
	<bean id="factoryBeanId"
            class="org.springframework.context.support.ClassPathXmlApplicationContext">
     <constructor-arg>
       <list>
         <value>sharebean.xml</value>
         </list>
     </constructor-arg>
   </bean>
	
</beans>

web.xml下的配置
<context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>beanRefFactory.xml</param-value>
    </context-param>
    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>factoryBeanId</param-value>
    </context-param>
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics