スプリングプロジェクトを統合するためのSpringプロファイルのロードの2つの方法


フィルタで実現
package com.beyond.nothing.filters;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/")
public class EncodingFilter implements Filter {
     

    private static Log log = LogFactory.getLog(EncodingFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
     
        //   spring     
        new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        log.info("EncodingFilter init success !!");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
     

    }

    @Override
    public void destroy() {
     

        log.info("EncodingFilter destroy success !!");
    }
}

web.xmlでモニターによるロードを実現します.
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

<!--                  -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

</web-app>