ContextConfigLocation java config
Я все еще плохо знаком с Java и весной, и у меня все еще есть некоторые проблемы с некоторыми связанными с контекстом темами. Я пытаюсь загрузить мои файлы конфигурации контекста из файла свойств. Я хотел обновить приложение Spring 3 до Spring 4. Ранее конфигурация работала нормально, но теперь я получаю сообщение об ошибке:
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
Конфигурационные файлы:
web.xml
<?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>
CustomServletListener.java
@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.
}
}
servlet.properties
security.configuration = WEB-INF/spring-security.xml WEB-INF/myweb-web-spring.xml WEB-INF/myServlet-servlet.xml
Я узнал, что applicationContext.xml
назначен на contextConfigLocation
параметр, прежде чем мой слушатель работает. Правильно ли я понимаю, что начиная с весны 4 dispatcherServlet теперь назначает значение по умолчанию contextConfigLocation, если оно не определено? Можете ли вы дать мне какие-либо идеи, как я могу достичь этого, не переводя всю конфигурацию в класс Java?