我有一个奇怪的问题。我有一个my-spring.xml文件我想添加到应用程序但由于某种原因我只能检测它是否在根上下文中。
以下内容未检测到根上下文中声明的my-spring.xml。
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-datasources.xml
/WEB-INF/my-spring.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>myservice</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webservice-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservice</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
</web-app>
如果我将其更改为此,则可以正常工作(请注意my-spring.xml位置的不同)。
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-datasources.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>myservice</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webservice-config.xml</param-value>
<param-value>/WEB-INF/my-spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservice</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
</web-app>
我真的不明白我做错了什么。如果我理解它的工作方式,当在根上下文中声明时(如第一个示例中所示),它应该由应用程序中的任何资源访问。在本示例中似乎不是这种情况,因为我无法从Controller / Endpoint类访问my-spring.xml文件中定义的属性。
答案 0 :(得分:1)
我认为您需要用逗号分隔多个contextConfigLocation
文件。根据这个答案:https://stackoverflow.com/a/2725413/116509
修改:尝试删除前面的/
,例如WEB-INF/my-datasources.xml
而不是/WEB-INF/my-datasources.xml
答案 1 :(得分:1)
尝试这样的事情
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/my-datasources.xml
classpath:/my-spring.xml
</param-value>
</context-param>
但是你必须在classpath中有这两个文件