2014-07-15 18 views
1

我有以下问题。我的多模块弹簧应用程序在启动时加载了两个不同的上下文。在jsp中使用不同上下文和SpringBeanAutowiringSupport的弹簧

上下文一加载了这段代码片段。

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

,并通过Web服务休息

<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/components_webservice.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

另外一个现在我用这个命令来使用jsp内自动装配豆一个jsp。

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 

这只允许自动装配主环境中定义的bean。我无法从REST Servlet声明的上下文中自动调用bean。有没有办法让这个上下文在jsp中可用?我知道一种方法是在主要上下文中声明所有的bean,但我希望它们保持分离。

问候, smoothny

+0

在此查看javadoc的ContextLoader http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/web/context/ContextLoader.html#getCurrentWebApplicationContext()。您可以使用“contextConfigLocation”参数配置位置。 – Priyesh

回答

1

不要放任何逻辑你的JSP中。在控制器中自动填充字段,并将生成的值传递给模型。

+0

这不会有什么区别,因为控制器也可能在不同的环境中运行。我的问题是如何从另一个上下文(contextConfigLocation配置一个vs默认的spring上下文)自动调用bean。 – smoothny

+0

您可以尝试使用导入标记,但这可能导致循环依赖上下文:。 – Stefan