【问题标题】:Where should I write my Spring MVC configuration class in a n-tiers architecture in a Maven project?在 Maven 项目的 n 层架构中,我应该在哪里编写 Spring MVC 配置类?
【发布时间】:2019-05-16 04:01:28
【问题描述】:

我正在尝试在n-tiers 网络应用程序中练习我在@9​​87654327@ 和Spring-MVC 中学到的知识。但是我发现在进行正确配置方面存在困难。

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

org.springframework.beans.factory.UnsatisfiedDependencyException

第一个项目:

第二个项目:

第三个项目:

您可能想知道为什么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)}

感谢您的宝贵时间。

【问题讨论】:

    标签: java spring maven spring-mvc


    【解决方案1】:

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

    希望对您有所帮助。

    【讨论】:

    • 我对依赖项进行了正确的配置。这不是我面临的问题。谢谢你的回答。
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 2010-10-02
    • 2011-03-30
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    相关资源
    最近更新 更多