我在一个小项目中使用了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和@Autow对其进行注释。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