ServerApplicationContext类与web.xml里配置servlet

  1. web.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-lineStatusMonitor-server.xml
    </param-value>
    </context-param>
    <!-- listener -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <servlet>
    <servlet-name>ServerApplicationContext</servlet-name>
    <servlet-class>com.sanmina.mes.dashboard.server.servlet.ServerApplicationContext
    </servlet-class>
    <load-on-startup>3</load-on-startup>
    </servlet>
    <!-- CXF REST -->
    <servlet>
    <display-name>CXFNonSpringJaxrsServlet</display-name>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet </servlet-class>
    <init-param>
    <param-name>jaxrs.serviceClasses</param-name>
    <param-value>
    com.sanmina.mes.dashboard.server.wsServer.MesrService,
    com.sanmina.mes.dashboard.server.wsServer.CacheWebService,
    <!-- com.sanmina.mes.dashboard.server.test.restful.MesrRestful, -->
    <!-- com.sanmina.mes.dashboard.server.test.restful.ProductLineRestful -->
    </param-value>
    </init-param>
    <init-param>
    <param-name>jaxrs.providers</param-name>
    <param-value>org.apache.cxf.jaxrs.provider.json.JSONProvider,
    org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter
    </param-value>
    <!-- <param-value>org.apache.cxf.jaxrs.provider.JSONProvider</param-value> -->
    </init-param>
    <init-param>
    <param-name>jaxrs.address</param-name>
    <param-value>/resources</param-value>
    </init-param>
    <init-param>
    <param-name>jaxrs.features</param-name>
    <param-value>org.apache.cxf.jaxrs.swagger.Swagger2Feature(basePath=/vision-server/rest/resources)</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
  2. ServerApplicationContext.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    package com.sanmina.mes.dashboard.server.servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import org.apache.log4j.Logger;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    public class ServerApplicationContext extends HttpServlet {
    protected static Logger logger = Logger.getLogger(ServerApplicationContext.class);
    private static final long serialVersionUID = 1L;
    private static ApplicationContext serverContext = null;
    public static String threadPropertiesPath ;
    public static String getThreadPropertiesPath() {
    return threadPropertiesPath;
    }
    public static void setThreadPropertiesPath(String threadPropertiesPath) {
    ServerApplicationContext.threadPropertiesPath = threadPropertiesPath;
    }
    @Override
    public void init() throws ServletException {
    super.init();
    javax.servlet.ServletContext servletContext = getServletContext();
    serverContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    setThreadPropertiesPath(getServletContext().getRealPath("WEB-INF")+"/thread.properties");
    }
    public static ApplicationContext getApplicationContext() {
    return serverContext;
    }
    public static void setApplicationContextForTest(ApplicationContext ctx ) {
    serverContext =ctx;
    }
    }
  3. 代码里面调用

    1
    2
    3
    LineStatusMonitorService lineStatusMonitorService = (LineStatusMonitorService) ServerApplicationContext
    .getApplicationContext().getBean("lineStatusMonitorService");