通过java设置servletConfig

时间:2011-06-22 09:53:31

标签: java spring servlets

web.xml中的

<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>aliases</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

现在我想在java中实例化servlet,如何设置contextConfigLocation

1 个答案:

答案 0 :(得分:0)

添加:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

在过滤器部分之前(您需要调整路径) 和

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在servlet部分之前