如何处理 Spring Boot Starter 模块化 Gradle 构建中定义的 Bean 缺失的依赖关系?

问题描述 投票:0回答:1

对于这个演示:

  • 我在
    event-starter
    模块中定义了一个 Bean 作为 Spring Boot Starter,我想在我的应用程序模块中使用它:
    application
    。据我所知,它在
    GreeterAutoConfiguration
    文件和
    resources/META-INF/spring.factories
  • 中有正确的配置,尽管过于简单
  • build.gradle
    模块中的
    application
    文件将
    event-starter
    作为依赖项引入,如下所示:
    
    implementation project(':event-starter')
    
    
  • 我同时使用
  • io.spring.dependency-management
    org.springframework.boot:spring-boot-configuration-processor
     注释处理器。
  • 我的构建按照我的预期进行编译。
但是,当我实际

运行应用程序时,Spring IoC 容器似乎无法发现我从启动器模块中拉入的 Bean,我不知道为什么:

Field greeter in com.example.app.StarterApplication required a bean of type 'com.example.starter.Greeter' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.starter.Greeter' in your configuration.
我大致使用这两个教程 [

12] 进行开发,这里是破坏性演示的链接:

  • https://github.com/Sideman4J/SpringStarterDemo
任何帮助识别缺失/错误的信息将不胜感激!

java spring gradle shared-libraries
1个回答
0
投票
如果是服务,需要用@Service注解

import org.springframework.stereotype.Service; @Service public class Greeter { public String greet() { return "hello"; } }
这应该可以解决你的错误

© www.soinside.com 2019 - 2024. All rights reserved.