如果我有一个java配置,我不太清楚如何为Spring Http Invoker配置web.xml文件。
这是config:
@Configuration
@Import(JdbcConfiguration.class)
@EnableWebMvc
public class HttpInvokerConfig {
@Bean
public HttpInvokerServiceExporter contactExporter(){
HttpInvokerServiceExporter contactExporter = new HttpInvokerServiceExporter();
contactExporter.setServiceInterface(ContactDao.class);
return contactExporter;
}
@Bean
public HttpInvokerProxyFactoryBean remoteContactService(){
HttpInvokerProxyFactoryBean remoteContactService = new HttpInvokerProxyFactoryBean();
remoteContactService.setServiceUrl("http://localhost:8080/experimental/ContactService");
remoteContactService.setServiceInterface(ContactDao.class);
return remoteContactService;
}
}
这里是web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Spring HTTP Invoker Sample</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>contactExporter</servlet-name>
<servlet-class>
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>contactExporter</servlet-name>
<url-pattern>/remoting/ContactService</url-pattern>
</servlet-mapping>
</web-app>
我需要在context-param中指定什么而不是xml的路径,是否可以在没有web.xml的情况下运行调用者(如servlet 3.0规范)?
答案 0 :(得分:0)
您的配置几乎正确。 您缺少的是两件事:
HttpInvokerProxyFactoryBean
的bean名称必须指定导出程序应在其上侦听的URL HttpInvokerProxyFactoryBean
必须注入了支持bean 此外,我强烈建议不要通过HTTP从正在导出它的同一应用程序中调用Bean。这样做的原因仅仅是因为它会弄乱依赖项注入,即remoteContactService
将注入到contactExporter
中。在应用程序中,通过HTTP进行的往返是没有用的,因为您可以直接调用Bean。
@Configuration
@Import(JdbcConfiguration.class)
@EnableWebMvc
public class HttpInvokerConfig {
@Bean("/remoting/ContactService")
public HttpInvokerServiceExporter contactExporter(ContactDao dao){
HttpInvokerServiceExporter contactExporter = new HttpInvokerServiceExporter();
contactExporter.setServiceInterface(ContactDao.class);
contactExporter.setService(dao);
return contactExporter;
}
}