gpt4 book ai didi

java - @Service 注解的 bean 未被 SpringBootApplication 拾取

转载 作者:行者123 更新时间:2023-12-01 09:17:50 28 4
gpt4 key购买 nike

我在一个小项目中使用了SpringBootApplication。架构很简单:

入口文件:

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class RptApp implements CommandLineRunner
{
@Autowired private RptService rptService;
@Override
public void run(String... args) throws Exception {
rptService.doStuff(){...};
}
}

其中RptService是一个接口(interface),它有一个实现:RptServiceImpl.java。就是RptServiceImpl.java被@Service注解了。

@Service
public class RptServiceImpl implements RptService {
@Override
public void doStuff();
}

我的理解是@SpringBootApplication已经嵌入了@ComponentScan,@EnableComponentScan(或类似的东西),@Configuration,以便rptService应该由容器自动连接。相反,它抛出了如下错误:

Description:

Field RptService in XXXX.RptApp required a bean of type 'xxx.xxx.xxx.RptService' that could not be found.


Action:

Consider defining a bean of type 'xxx.xxx.xxx.RptService' in your configuration.

我知道如何根据提示找到解决方法,但除此之外。

我确实编写了另一个简单的类 Client 并在主文件中使用 @Component 和 @Autowired 注释它。 Spring 没有遇到任何问题。

我的 pom 文件的相关部分如下所示:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
....

有人可以透露一下吗?

最佳答案

RptService 应该在主入口文件的子目录中选择 Spring @ComponentScan

关于java - @Service 注解的 bean 未被 SpringBootApplication 拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407130/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com