I apologize if this has been asked and answered, but I've been unable to find the solution. In short, I thought I could use Spring in my application without ever having Java code which imports classes from the Spring API.
In my test class, I use the following code to access a bean, defined in
applicationContext.xml.
XmlBeanFactory mBeanFactory = new XmlBeanFactory(new
ClassPathResource("applicationContext.xml", getClass()));
TestObject test = (TestObject)mBeanFactory.getBean("testObject");
String strSomething = test.getSomething();
System.out.println("strSomething = " + strSomething);
This works fine, in that strSomething has alreaady been injected, since it
was specified in applicationContext.xml.
However, I'm unsuccessful in setting this up in a servlet container. I used
the same applicationContext.xml, and made reference to it from web.xml using
the following:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</lis
tener-class>
</listener>
The part that I haven't figured out yet is how to access, get a new
TestObject *without explicitly using the Spring API in code*. I presume its
unlikely/unreasonable to expect that Spring, once loaded into the context
(which does happen as confirmed in log files) will watch for any usage/request for a TestObject and
instantiate as needed.
But alas, I've searched far and wide, and only find reference to using through
Struts/MVC style code which again, is coded to use the Spring API (e.g. WebApplicationContextUtils).
Please help, is there a way to do this without having the dependency on Spring in Java (declaratively in XML is great, just not java).


Reply With Quote