What is the best way to get a reference to a spring bean in the backend layers?
- by java_pill
I have two spring config files and I'm specifying them in my web.xml as in below.
web.xml snippet
..
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/domain-context.xml WEB-INF/classes/client-ws.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
..
From my domain object I have to invoke a Web Service Client and in order to get a reference to the Web Service client I do this:
ApplicationContext context = new ClassPathXmlApplicationContext("client-ws.xml"); //b'cos I don't want to use WebApplicationContextUtils
ProductServiceClient client = (ProductServiceClient) context.getBean("productClient");
..
client.find(prodID); //calls a Web Service
..
However, I have concerns that looking up the client-ws.xml file and getting a reference to the ProductServiceClient bean is not efficient. I thought of getting it using WebApplicationContextUtils. However, I don't want my domain objects to have a dependency on the ServletContext (a web/control layer object) because WebApplicationContextUtils depends on ServletContext. What is the best way to get a reference to a spring bean in the backend layers?
Thanks!