What is the best way to get a spring bean reference on indoor units?

I have two spring configuration files and I specify them in my web.xml as shown 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 call the web service client and to get the link 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 finding the client-ws.xml file and getting a link to the ProductServiceClient bean is inefficient. I was thinking of getting it using WebApplicationContextUtils. However, I do not want my domain objects to depend on ServletContext (web / control layer object), because WebApplicationContextUtils depends on ServletContext. What is the best way to get a spring bean reference on indoor units?

Thank!

+3
3

Spring applicationContext. Spring ApplicationContextAware. bean .

:

public class ContextAwareFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public String getConfigValue() {
        return (String)applicationContext.getBean("config-value");
    }
}
+3

bean? " , " - . , Spring DI .

+1

:

new ClassPathXmlApplicationContext("client-ws.xml"); 

web.xml. - , .

, bean - , Spring.

" "? , "Spring -aware", WS- :

  • explicitly inserting it from the context of the application (possibly not applicable to the domain object, but mentioned for completeness);

  • using @Autowired; or

  • with the help of @Configurable.

0
source

Source: https://habr.com/ru/post/1742627/


All Articles