当前位置: 首页 > 知识库问答 >
问题:

Spring Boot应用程序不接受引发异常的控制器类

皇甫琛
2023-03-14

我已经通过在pom.xml中添加插件和spring引导依赖项将一个现有的spring应用程序转换为spring引导应用程序。这是我的pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- tag::actuator[] -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- end::actuator[] -->

    <!-- Spring Security -->  
    <dependency>  
      <groupId>org.springframework.security</groupId>  
      <artifactId>spring-security-core</artifactId>  
      <version>3.2.5.RELEASE</version>  
    </dependency>  

    <dependency>  
      <groupId>org.springframework.security</groupId>  
      <artifactId>spring-security-web</artifactId>  
      <version>3.2.5.RELEASE</version>  
    </dependency>  

    <dependency>  
      <groupId>org.springframework.security</groupId>  
      <artifactId>spring-security-config</artifactId>  
      <version>3.2.5.RELEASE</version>  
    </dependency>
    <!-- Spring Security --> 

    <!-- odbc -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.2.0</version>
    </dependency>
    <!-- odbc -->

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

下面是我的controller类:

@Controller
@ComponentScan("com.eg.example.controller")
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private DatabaseDAO dao;

    /*some code */

}
public class DatabaseDAO {
    /*dao sepcific code*/
}

现在,在运行maven安装之后,jar被构建在目标文件夹中,当我从命令提示符运行jar时,我会收到以下错误:

我无法找出错误在哪里,请帮助我解决这个问题。这是我的servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

        <context:component-scan base-package="com.eg.example.*" />

</beans:beans>

共有1个答案

谢锦程
2023-03-14

您应该在databaseDao类上面注释@repository。由于spring上下文无法找到bean名称DatabaseDao将其注入到HomeController组件中而导致的错误。

 类似资料: