簡體   English   中英

我應該在Maven項目的n層體系結構中將Spring MVC配置類編寫在哪里?

[英]Where should I write my Spring MVC configuration class in a n-tiers architecture in a Maven project?

我正在嘗試使用SpringSpring-MVCn-tiers Web應用程序中練習我學到的東西。 但我發現做正確配置有困難。

所以我正在使用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)}

謝謝你的時間。

您是否已將第一個和第二個項目放入第三個項目的依賴項中。 這是我的項目構建示例,包含2個模塊。 在此輸入圖像描述 這就是我配置項目的方式:1。將核心模塊放入我的app-module依賴項中 在此輸入圖像描述 2.從Main調用它 在此輸入圖像描述

希望它可以提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM