ContextConfigLocation Java配置

时间:2018-07-23 08:04:42

标签: java spring context-configuration

我对Java和Spring还是陌生的,并且在与上下文相关的主题上仍然遇到一些麻烦。我正在尝试从属性文件加载上下文配置文件。我想将spring 3应用程序升级到spring4。以前的配置可以正常工作,但是现在出现错误:

debug

配置文件:

web.xml

INFO  [localhost-startStop-1] [CustomServletListener] Context Propery: :classpath:applicationContext.xml
ERROR [localhost-startStop-1] [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

CustomServletListener.java

<?xml version="1.0" encoding="iso-8859-1"?>

<web-app id="myweb" version="2.4" 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">

    <display-name>myweb</display-name>

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>

    <login-config>
        <auth-method>BASIC</auth-method>
    </login-config>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>\/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>\/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>\/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>


    <context-param>
        <param-name>defaultHtmlEscape</param-name>
        <param-value>true</param-value>
    </context-param>

    <listener>
      <listener-class>
         com.mypackage.web.servlets.CustomServletListener
      </listener-class>
   </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/</url-pattern>

    </servlet-mapping>

</web-app> 

servlet.properties

 @WebListener
 @EnableWebSecurity
 @EnableGlobalMethodSecurity
 public class CustomServletListener implements ServletContextListener
 {

    Logger LOG = Logger.getLogger(CustomServletListener.class);

        @Override
        public void contextInitialized(ServletContextEvent event) {
            ServletContext servletContext = event.getServletContext();
            Properties  props = new Properties();
            try
            {
              File files = new File(System.getProperty("CONFIG_DIR")+"/servlet.properties");
               props.load(new FileInputStream(files));
            }
            catch (IOException e)
            {
                LOG.error("Error while loading property: " + e.getMessage(),e);
            }
            boolean flag = servletContext.setInitParameter("contextConfigLocation",  props.getProperty("security.configuration"));
            LOG.info("Flag: " + flag);
            LOG.info("Context Propery: :" + servletContext.getInitParameter("contextConfigLocation"));
        }

        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // Empty.
        }



 }

我发现在我的侦听器运行之前,security.configuration = WEB-INF/spring-security.xml WEB-INF/myweb-web-spring.xml WEB-INF/myServlet-servlet.xml 已分配给applicationContext.xml参数。我是否正确理解,因为未定义Spring 4 dispatcherServlet现在会分配contextConfigLocation的默认值?您能否给我任何想法,而又不将整个配置移到Java类的情况下如何实现呢?

0 个答案:

没有答案