Servlet [dispatcher]的Servlet.init()抛出异常

时间:2018-12-29 18:54:33

标签: java spring

我试图在另一个jar文件中访问DAO类,我已经在pom.xml中添加了它的依赖关系,并试图通过当前模块中的“页面控制器”来访问它,但是我遇到了异常

我尝试在组件扫描中检查名称,检查自动装配对象的名称

dispatcher-servlet.xml

<context:component-scan base-package="com.apurv.onlineshopping.controller"/>
<context:component-scan base-package="com.apurv.shoppingbackend.daoImpl "/>


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix"  value="/WEB-INF/views/"></property>
 <property name="suffix"  value=".jsp"></property>



 </bean>    

 <!-- loading static resources -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/assets/" mapping="/resources/**"></mvc:resources>
</beans>        

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

页面控制器

package com.apurv.onlineshopping.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.apurv.shoppingbackend.dao.CategoryDAO;

@Controller
public class PageController {

    @Autowired
    private CategoryDAO categoryDao;

    @RequestMapping(value= {"/","/home","/index"})
    public ModelAndView index() {
        ModelAndView mv= new ModelAndView("page");
        mv.addObject("title","Home");

        //passing category list from back end
        mv.addObject("categories", categoryDao.list());

        mv.addObject("userClickHome",true);
        return mv;
    }

    @RequestMapping(value="/about")
    public ModelAndView about() {
        ModelAndView mv= new ModelAndView("page");
        mv.addObject("title","About Us");
        mv.addObject("userClickAbout",true);
        return mv;
    }

    @RequestMapping(value="/contact")
    public ModelAndView contact() {
        ModelAndView mv= new ModelAndView("page");
        mv.addObject("title","Contact Us");
        mv.addObject("userClickContact",true);
        return mv;
    }
}

其他jar DAO

package com.apurv.shoppingbackend.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.apurv.shoppingbackend.dao.CategoryDAO;
import com.apurv.shoppingbackend.dto.Category;

@Repository
public class CategoryDAOImpl implements CategoryDAO{

    private static List<Category> categories=new ArrayList<>();

    static {
        Category category=new Category();

        //first category
        category.setId(1);
        category.setName("Televison");
        category.setDescription("this is a flat screen tv");
        category.setImageURL("CAT_1.png");


        categories.add(category);


        /*
         * //second category category=new Category(); category.setId(2);
         * category.setName("Mobile"); category.setDescription("this is a apple phone");
         * category.setImageURL("CAT_2.png");
         * 
         * 
         * categories.add(category);
         * 
         * //third category
         * 
         * category=new Category();
         * 
         * category.setId(3); category.setName("fridge");
         * category.setDescription("this is a double door fridge");
         * category.setImageURL("CAT_3.png");
         * 
         * 
         * categories.add(category);
         * 
         * //fourth category
         * 
         * category=new Category(); category.setId(4);
         * category.setName("washing machine");
         * category.setDescription("this is an automatic washing machine ");
         * category.setImageURL("CAT_4.png");
         * 
         * 
         * categories.add(category);
         * 
         */     
    }

    @Override
    public List<Category> list() {
        // TODO Auto-generated method stub
        return categories;
    }

}

HTTP状态500 –内部服务器错误     类型异常报告

Message Servlet.init() for servlet [dispatcher] threw exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet.init() for servlet [dispatcher] threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
Root Cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pageController' defined in file [C:\Users\apurv\fullStackPractice\shopping\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\onlineshopping\WEB-INF\classes\com\apurv\onlineshopping\controller\PageController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [com.apurv.onlineshopping.controller.PageController] for resource metadata: could not find class that it depends on
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:526)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

pom.xml,其中我已将其他模块作为依赖项

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.apurv</groupId>
      <artifactId>onlineshopping</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>onlineshopping Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <properties>
      <javaee.version>7.0</javaee.version>
      <springmvc.version>4.3.6.RELEASE</springmvc.version>
      </properties>

      <dependencies>

      <!--adding backend   -->
      <dependency>
      <groupId>com.apurv</groupId>
      <artifactId>shoppingbackend</artifactId>
     <version>0.0.1-SNAPSHOT</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.springframework/spring- 
        webmvc -->


      <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
       <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>${javaee.version}</version>
        <scope>provided</scope>
    </dependency>




        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springmvc.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
       <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


      </dependencies>
      <build>
      <plugins>
      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>



    <source>1.8</source>
      <target>1.8</target>
      </configuration>
      </plugin>
      </plugins>
        <finalName>onlineshopping</finalName>
      </build>
    </project>

pom.xml of the module which i have included in the above


<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.apurv</groupId>
  <artifactId>shoppingbackend</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>shoppingbackend</name>
  <url>http://maven.apache.org</url>

  <properties>
  <javaee.version>7.0</javaee.version>
  <springmvc.version>4.3.6.RELEASE</springmvc.version>
  </properties>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->


  <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>${javaee.version}</version>
    <scope>provided</scope>
</dependency>




    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${springmvc.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


  </dependencies>

  <build>
  <plugins>
  <plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
  <source>1.8</source>
  <target>1.8</target>
  </configuration>
  </plugin>
  </plugins>
    <finalName>shoppingbackend</finalName>
  </build>
</project>

1 个答案:

答案 0 :(得分:0)

问题的根源在这里:

  

无法自检bean类   资源的[com.apurv.onlineshopping.controller.PageController]   元数据:找不到它依赖的类

DI容器找不到另一个.jar中定义的类 为了解决这个问题,让我们看一下代码:

您注入了DAO组件:

@Controller
public class PageController {

    @Autowired
    private CategoryDAO categoryDao;

可以在web.xml中定义的压缩包(不是.jars)中找到:

<context:component-scan base-package="com.apurv.onlineshopping.controller"/>
<context:component-scan base-package="com.apurv.shoppingbackend.daoImpl "/>

因为我发现您的两个模块是单独存储的,所以构建系统没有信息,除非您手动将其插入,否则它们无法找到依赖于网上购物的信息。

我的解决方案是:将两个独立模块组织到父模块pom中:

此处是父级 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project ...

    <groupId>com.apurv</groupId>
    <artifactId>so</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>shoppingbackend</module>
        <module>onlineshopping</module>
    </modules>

</project>

接下来在您的模块中为父对象添加一个引用: 网上购物的 pom.xml

<project ...
    <modelVersion>4.0.0</modelVersion>
    <!--unnecessary <groupId>com.apurv</groupId>-->
    <artifactId>onlineshopping</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineshopping</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.apurv</groupId>
        <artifactId>so</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    ... tail of your code is unchanged ...

和shoppingbackendend的 pom.xml

<project ...
    <modelVersion>4.0.0</modelVersion>
    <!--unnecessary  <groupId>com.apurv</groupId>-->
    <artifactId>shoppingbackend</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>shoppingbackend</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.apurv</groupId>
        <artifactId>so</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

 ... tail of your code is unchanged ...

project outline

下一个构建 root 模块:

mvn package

or package by mouse

其余代码未更改,您可以找到完整的仓库:https://github.com/enginetag/so-apurv