【发布时间】:2017-03-17 08:52:51
【问题描述】:
我在一个小项目中使用了 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 是一个接口,它有一个实现: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>
....
任何人都可以解释一下吗?
【问题讨论】:
-
你的类只有方法签名,你是如何编译你的应用程序的? public void doStuff();
-
RptApp和RptService是在同一个包还是在不同的包?
-
文件的项目结构是什么?
-
@kuhajeyan 这是一个错字 - 我已经更新了帖子。
-
@Issam EL ATIF 你说的太对了——我太傻了。 RptService 不在主条目文件的子目录中。现在问题已经解决了。
标签: java spring maven spring-boot