2016-03-31 88 views
0

我得到以下錯誤,我研究了由於缺少依賴或至少那是什麼答案是類似的問題。我相信我對Spring框架擁有所有必要的依賴關係,但它一直在拋出錯誤。我已經在服務器和項目上運行乾淨,但無濟於事。任何輸入都會很好,因爲我對Spring和Java企業應用程序非常陌生。春天沒有類當發現依賴項時發現異常

org.apache.catalina.core.ApplicationContext log SEVERE: StandardWrapper.Throwable 
java.lang.NoClassDefFoundError: 
org/springframework/context/support/FileSystemXmlApplicationContext 
at mycalendar.servlets.EventServlet.init(EventServlet.java:53) 
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 

Mar 31, 2016 6:13:41 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet eventServlet java.lang.NoClassDefFoundError: org/springframework/context/support/FileSystemXmlApplicationContext 
at mycalendar.servlets.EventServlet.init(EventServlet.java:53) 
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 
在使用maven在我宣佈Spring框架的情況下我的POM文件

但是,如下圖所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>MyCalendar</groupId> 
    <artifactId>MyCalendar</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <dependencies> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>3.2.3.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-oxm</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc-portlet</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
    <version>1.4</version> 
    </dependency> 
    <dependency> 
     <groupId>hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>1.8.0.7</version> 
    </dependency> 

    </dependencies> 


    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.3</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.6</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

錯誤是從我的servlet內部發出:

@Override public void init(ServletConfig config)throws ServletException { super.init(config);

ApplicationContext context = new FileSystemXmlApplicationContext("spring.xml"); <------here 

Calendar cal = new GregorianCalendar(); 

Event eventOne = new Event(1, "event 1", "john smith", "12:00am","Fullerton, CA",cal); 
Event eventTwo = new Event(2, "event 1", "jane smith", "1:00pm","Placentia,CA",cal); 
Event eventThree = new Event(3, "event 3", "jules smith", "2:00pm","Brea, CA",cal); 

//Add Dummy Data 
eventsDatabase.add(eventOne); 
eventsDatabase.add(eventTwo); 
eventsDatabase.add(eventThree); 

} 

項目結構:

enter image description here

+0

檢查你正確導入與否? –

+0

@subburoyal你是什麼意思? – wmurmann

回答

1

眼看着你的代碼,我可以看到你定義一個pom.xml但由於逸岸你沒有,你的項目是不是一個Maven項目項目結構如下圖所示

src 
- main 
    - java 
    - ..... 
    - resource 
    - .... 
    - webapp 
     WEB-INF 
     - .... 
     ..... 
- test 
    - java 
     - ..... 
    - resource 
     - .... 

爲此設置的POM依賴包裝等等,但你cant'use Maven的可能是你使用Eclipse IDE用於構建戰爭,並且eclipse可能沒有在WEB-INF下的lib文件夾下正確包裝你的依賴項。你已經手動設置eclipse pakage正確的depndency。

我的建議是重構你的代碼,用maven的好處與我上面描述的結構,讓eclipse知道你有一個maven項目設置maven的本質,並且一般不使用FileSystemXmlApplicationContext作爲web應用程序。你用前人的精力像這樣的結構:

配置你的Spring Web上下文:getRootConfigClasses爲@Configuration類的根Spring上下文,並getServletConfigClasses爲您的Web上下文

public class PhoneBookServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] {PhonBookApplicationContext.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] {PhoneBookMvcContext.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] {"/"}; 
    } 
} 
的你@Configuration類

配置這樣的你春季Web上下文:此示例中,我需要從我的開源項目電話簿但關鍵點ispiration

@Configuration 
@EnableTransactionManagement 
@ComponentScan(basePackages = {"it.valeriovaudi.web.controller","it.valeriovaudi.web.rest"}) 
@EnableWebMvc 
public class PhoneBookMvcContext extends WebMvcConfigurerAdapter { 

// Thymeleaf Config 
/* 
* <bean id="templateResolver" 
     class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" 
     p:prefix="/WEB-INF/templates/" 
     p:templateMode="HTML5" 
     p:suffix=".html"/> 
    <bean id="templateEngine" 
     class="org.thymeleaf.spring4.SpringTemplateEngine" 
     p:templateResolver-ref="templateResolver"> 
     <property name="additionalDialects"> 
     <set> 
     <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/> 
     </set> 
     </property> 
    </bean> 
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver" 
      p:templateEngine-ref="templateEngine"/> 
    */ 
    @Bean 
    public ServletContextTemplateResolver templateResolver(){ 
     ServletContextTemplateResolver servletContextTemplateResolver = new ServletContextTemplateResolver(); 
     servletContextTemplateResolver.setPrefix("/WEB-INF/templates/"); 
     servletContextTemplateResolver.setSuffix(".html"); 
     servletContextTemplateResolver.setTemplateMode("HTML5"); 

     return servletContextTemplateResolver; 
    } 

    @Bean 
    public SpringTemplateEngine templateEngine(){ 
     SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine(); 
     springTemplateEngine.setTemplateResolver(templateResolver()); 
     springTemplateEngine.setAdditionalDialects(Collections.singleton(new SpringSecurityDialect())); 
     return springTemplateEngine; 
    } 

    @Bean 
    public ThymeleafViewResolver thymeleafViewResolver(){ 
     ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); 
     thymeleafViewResolver.setTemplateEngine(templateEngine()); 

     return thymeleafViewResolver; 
    } 

    /* 
    Message .properties sources 
    <bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
    p:basenames="WEB-INF/messages/messages"/> 
    */ 

    @Bean 
    public MessageSource messageSource(){ 
     ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource(); 
     reloadableResourceBundleMessageSource.setBasenames("WEB-INF/messages/messages"); 

     return reloadableResourceBundleMessageSource; 
    } 
    /* 
    <mvc:resources mapping="/resources*//**" location="web-resources/, classpath:/META-INF/resources/webjars/" /> 
    <mvc:resources mapping="/favicon.ico" location="web-resources/img/favicon.jpg" /> 
    */ 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     super.addResourceHandlers(registry); 

     registry.addResourceHandler("/resources/**").addResourceLocations("web-resources/").addResourceLocations("classpath:/META-INF/resources/webjars/"); 
     registry.addResourceHandler("/favicon.ico").addResourceLocations("web-resources/img/favicon.jpg"); 
    } 

} 

是,你必須配置你的背景下像上面independ現在形成配置的bean,舉例說明你配置的類的類型以及你需要使用maven的項目結構。

我希望能幫助你