What is the best way to get a reference to a spring bean in the backend layers?

Posted by java_pill on Stack Overflow See other posts from Stack Overflow or by java_pill
Published on 2010-04-24T23:54:57Z Indexed on 2010/04/25 0:03 UTC
Read the original article Hit count: 171

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about spring

Related posts about spring-framework