Spring-mvc中的惰性类加载

时间:2015-07-06 14:45:19

标签: java spring spring-mvc servlets wildfly-8

我在Wildfly 8上有一个项目。所以我有一个战争应用程序和许多sar应用程序。在战争中,我有管理实体的控制器,这些实体在sar中处理。 MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd" default-lazy-init="true">
    <context:component-scan base-package="com.a1s.assist.web"/>
    <mvc:annotation-driven/>

</beans>

的web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/mvc/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>parentContextKey</param-name>
    <param-value>assist.context</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>


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

我的servlet代码:

package com.a1s.assist.web.broadcast.controller;

/**
 * @author Alexey Belov
 */

import com.a1s.assist.broadcast.manager.BroadcastZoneManager;
import com.a1s.assist.broadcast.obj.BroadcastZone;
import com.a1s.assist.web.settings.controller.BaseSpringController;
import com.a1s.assist.web.settings.util.Messages;
import com.a1s.util.misc.ExceptionUtil;
import com.a1s.web.action.BaseAction;
import com.a1s.web.controller.FrontJsonController;
import org.jboss.logging.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Lazy
@Controller
@RequestMapping(method = {RequestMethod.POST}, value = "/broadcast/zone")
public class BroadcastZoneController extends BaseSpringController {


    private Logger log = Logger.getLogger(BroadcastZoneController.class);
    @Autowired
    private BroadcastZoneManager broadcastZoneManager;

    @ResponseBody
    @RequestMapping(value = "/list")
    public void list(HttpServletRequest request,
                     HttpServletResponse response
    ) throws IOException {
        final ServletOutputStream out = response.getOutputStream();
        try {
            log.info("list zones");
            final JSONObject requestJson = (JSONObject) request.getAttribute(FrontJsonController.REQUEST_JSON_PARAMETER);
            final JSONObject parseJson = requestJson.getJSONObject("request").getJSONObject("params").getJSONObject(BroadcastZone.WRAP);
            final String operatorId = parseJson.getString("operatorId");
            final String parentId = parseJson.getString("parentId");
            final List<BroadcastZone> all = broadcastZoneManager.listChildsTree(parentId, operatorId);
            final List list = BaseAction.toJson(all);
            BaseAction.sendJsonOk(out, list);
        } catch (Exception e) {
            log.error(e);
            BaseAction.sendJsonError(out, Messages.IO_ERROR, "Unknown error: " + ExceptionUtil.getStackTrace(e), new JSONObject());
        }
    }
}

因此,当我尝试在没有包含BroadcastZone的模块的情况下启动wildfly时,我得到了异常:

Failed to start service jboss.undertow.deployment.default-server.default-host./ROOT: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./ROOT: Failed to start service
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_05]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_05]
        at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/a1s/assist/broadcast/obj/BroadcastZone
        at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
        ... 3 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/a1s/assist/broadcast/obj/BroadcastZone
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
        at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
        at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193)
        ... 7 more
Caused by: java.lang.NoClassDefFoundError: com/a1s/assist/broadcast/obj/BroadcastZone
        at java.lang.Class.getDeclaredMethods0(Native Method) [rt.jar:1.8.0_05]
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2688) [rt.jar:1.8.0_05]
        at java.lang.Class.getDeclaredMethods(Class.java:1962) [rt.jar:1.8.0_05]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:571)
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:488)
        at org.springframework.web.method.HandlerMethodSelector.selectMethods(HandlerMethodSelector.java:57)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:169)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        ... 21 more
Caused by: java.lang.ClassNotFoundException: com.a1s.assist.broadcast.obj.BroadcastZone from [Module "deployment.ROOT.war:main" from Service Module Loader]
        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389)
        at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134)
        ... 33 more

如果请求在我的映射上发布,我该怎么做才能生成弹簧加载控制器。并且NoClassDefFound错误只会在那时引发,但在启动时不会引发?

1 个答案:

答案 0 :(得分:0)

我终于意识到为什么会发生错误。事情是在load-on-startup元素。我认为当值为零时它不会在启动时加载。今天我发现了:

  

load-on-startup元素表明这一点               应该加载servlet(实例化并具有               它在网络启动时的init()被调用               应用。这些的可选内容               element必须是一个表示顺序的整数               应该加载servlet。如果值               是负整数,或者元素不是               目前,容器可以自由加载servlet               无论什么时候选择。如果值是正数               整数或0,容器必须加载和               在应用程序初始化servlet               部署。容器必须保证               加载标有较低整数的servlet               在用更高整数标记的servlet之前。该               容器可以选择装载的顺序               具有相同加载启动值的servlet。

这就是我在启动时获得NoClassDefFound的原因。