【问题标题】:RestController required a service bean with spring boot & multi modulesRestController 需要一个带有 spring boot 和多模块的服务 bean
【发布时间】:2017-04-10 05:17:16
【问题描述】:

我将我的 Spring Boot 项目作为单个 Maven 项目进行了测试,并且一切正常。 我决定将我的应用程序拆分为子模块。存储库、服务和 Spring Boot Web 模块。自动接线停止工作。 并出现以下错误:

20:12:08.052 [main] WARN  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRestController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.plendo.service.interfaces.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
20:12:08.068 [main] WARN  o.s.boot.SpringApplication - Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
20:12:08.243 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - ``
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in org.plendo.ui.controller.UserRestController required a bean of type 'org.plendo.service.interfaces.UserService' that could not be found.

【问题讨论】:

  • 信息不够。你有没有例如改变你的包结构?你的项目结构和配置类现在是什么样子的?

标签: spring rest maven spring-mvc spring-boot


【解决方案1】:

项目结构:父项目包含jpa模块、服务模块和web模块(spring boot app)

  • spring boot 应用类:web 模块

    package org.plendo.ui;     
    @SpringBootApplication public class App { public static void main(String[] args) { 
    @SpringBootApplication
    public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
        }
    
  • RestController 类:网络模块

    package org.plendo.ui.controller;
    import org.plendo.jpa.entity.User;
    
    import org.plendo.service.interfaces.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    //@ComponentScan("org.plendo.service.interfaces")
    public class UserRestController {
    
    @Autowired
    //@Qualifier("userService")
    private UserService userService;
    
    @RequestMapping(value = "/saveUser")
    public User saveUser(User u) {
        return userService.saveUser(u);
    }
    }
    
  • UserServiceImpl : 服务模块

    import org.apache.log4j.Logger;
    import org.plendo.jpa.entity.User;
    import org.plendo.jpa.entity.UserRepository;
    import org.plendo.service.interfaces.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl implements UserService {
    
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    UserRepository userRepository;
    
    @Override
    public User saveUser(User u) {
        return userRepository.save(u);
    }
    

    }

  • UserService接口:服务模块

    package org.plendo.service.interfaces;
    
    import org.plendo.jpa.entity.User;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    public interface UserService {
    
    public User saveUser(User u);
    }
    
  • UserRepository 接口:jpa 模块

    package org.plendo.jpa.entity;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface UserRepository  extends JpaRepository<User, String>{
    
    }
    

    还有其他事情要做吗?

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 2016-01-15
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2021-12-10
    • 2019-02-09
    相关资源
    最近更新 更多