We've the same problem. I set up the back end with Spring, which runs in a Servlet Container (Tomcat). To load the context I do this:
Code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/myComponent-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
The servlet looks like this:
Code:
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
ApplicationContext appContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletConfig.getServletContext());
exchanger = (ICWExchanger) appContext.getBean("cwExchanger");
webServices = (IWebServices)appContext.getBean("webServicesCW");
//Don't know if this is a good solution
ExchangerFactory.setApplicationContext(appContext);
}
As hay7777 already mentioned, he want to access Spring managed beans in Scripts. We work with Beanshell. My workaround is the ExchangerFactory, which gets the application Context.
Code:
public class ExchangerFactory {
private static ApplicationContext appContext;
public static void setApplicationContext(ApplicationContext ctx){
appContext = ctx;
}
public static Object getService(String service){
return appContext.getBean(service);
}
The Exchanger has a static method to access the Context and returns the required Bean. If I use a Spring managed bean in the script I call:
Code:
Exchanger.getService("myService").
This method delegates to the appContext and then again gets the required object. I think this way is similar to the one of hay7777?
Is it the best way?
markus