我正在尝试在 n 层
Web 应用程序中练习使用 Spring
和 Spring-MVC
所学到的知识。但我发现进行正确配置很困难。
所以我正在使用Maven
。到目前为止我创建了 3 个项目。第一个负责通用 Hibernate
DAO 实现。第二个是我的业务逻辑层,其中有我的 DAO 类和服务。第三个是我的 Controller 所在的位置。我想使用 Spring 依赖注入(inject)来管理我的服务和 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. 将 core-module 放入我的 app-module 依赖项中 2.从Main调用它
希望能帮到你。
关于java - 在 Maven 项目的 n 层架构中,我应该在哪里编写 Spring MVC 配置类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160646/