java - 在 Maven 项目的 n 层架构中,我应该在哪里编写 Spring MVC 配置类?

标签 java spring maven spring-mvc

我正在尝试在 n 层 Web 应用程序中练习使用 SpringSpring-MVC 所学到的知识。但我发现进行正确配置很困难。

所以我正在使用Maven。到目前为止我创建了 3 个项目。第一个负责通用 Hibernate DAO 实现。第二个是我的业务逻辑层,其中有我的 DAO 类和服务。第三个是我的 Controller 所在的位置。我想使用 Spring 依赖注入(inject)来管理我的服务和 DAO 实例。但无论我把配置类放在哪里,我都会收到错误:

org.springframework.beans.factory.UnsatisfiedDependencyException

第一个项目:

First project structure

第二个项目:

Second project structure

第三个项目:

Third project structure

您可能想知道为什么 AppConfig 出现在两个项目中,这只是为了调试目的。我不知道到底应该把它放在哪里。

我的AppConfig类:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.controllers", "com.services", "com.dao" })
public class AppConfig {

}

我的SpringMvcDispatcherInitializer类:

public class SpringMvcDispatcherInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

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

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

}

Maven 服务器上运行 pfa-web 项目时收到的错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'touristController': Unsatisfied dependency expressed through field 'touristService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'touristServiceImp': Unsatisfied dependency expressed through field 'touristDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'touristDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

感谢您的宝贵时间。

最佳答案

您是否将第一个和第二个项目放入第三个项目的依赖项中。这是我的项目构建示例,包含 2 个模块。 enter image description here 这就是我配置项目的方式: 1. 将 core-module 放入我的 app-module 依赖项中 enter image description here 2.从Main调用它 enter image description here

希望能帮到你。

关于java - 在 Maven 项目的 n 层架构中,我应该在哪里编写 Spring MVC 配置类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160646/

相关文章:

java - 为命令行 Java 应用程序创建可运行的 JAR 文件

java - 根据出现次数获取子字符串,正则表达式java

java - 与Spring Cloud Sleuth一起使用时,Spring Integration错误 channel 处理中断

java - 两个 Spring WebFlow 项目共享数据的最佳方式

java - Maven:将资源复制到动态目录

java - 为什么我的Web应用程序NoClassDefFoundError : org/aspectj/lang/reflect/AjTypeSystem

java - 如果 jar 文件大于特定大小,Applet 无法在 Windows 7 上使用 Java 1.7 加载

spring - 通过属性禁用 CSRF 保护

maven - Tycho/Maven 的配置问题 : "Tycho build extension not configured"

java - 依赖范围系统的 Sonar 违规