在 Spring ,当我尝试从我的 BlogController
执行操作时,我试图解决 Unresolved Bean 异常
:
@Autowired BlogService blogService;
- 我正在使用
org.springframework.stereotype.Service
服务注释。 - 我的
ApiApplication
应用程序类使用@ComponentScan("com.mypackage")
进行注释。 - 服务实现是用
@Service
注释的,位于com.mypackage.service.blog.BlogService"
- 该服务无法
Autowired
,但它是由该服务使用的@Repository
,并且位于com.mypackage.repository.blog.BlogRepository
code> 可以由 Controller 导入。
我的应用程序类如下所示:
package com.mypackage;
import com.mypackage.core.Core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({
"com.mypackage",
"com.mypackage.service.blog"
})
public class ApiApplication {
private static final Logger logger = LoggerFactory.getLogger(ApiApplication.class);
public static void main(String[] args) throws Exception {
org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
SpringApplication.run(ApiApplication.class, args);
logger.info("Application started!");
}
}
这是我的com.mypackage.controller.blog.BlogController
:
@RestController
@RequestMapping("/blogs")
public class BlogController {
@Autowired
private BlogService blogService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
Long create(@RequestBody Blog blog) {
blogService.insert(blog);
return blog.getId();
}
我的com.mypackage.service.blog.BlogService
类:
public interface BlogService extends CrudService<Blog, Long> {
}
我的com.mypackage.service.blog.impl.BlogServiceImpl
类:
@Service
@UserManagementTx
public class BlogServiceImpl extends AbstractCrudService<BlogRepository, Blog, Long> {
@Autowired
public BlogServiceImpl(BlogRepository repository) {
super(repository);
}
}
我已经打开了调试日志,并且正在尝试找到一些提示为什么服务未导入。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mypackage.service.blog.BlogService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我应该将一些特定的类路径设置为 DEBUG 并将另一个设置为 INFO 吗?我在当前的调试日志中没有看到服务创建和类路径。
最佳答案
点#1
@ComponentScan
在这里不需要,只需将其从应用程序的主类中删除,即 ApiApplication
即可工作。
点#2
正如我们所见,BlogServiceImpl
没有实现 BlogService
,这意味着 BlogService
没有具体实现,因此 Bean
> 无法创建。
你需要实现BlogServiceImpl
接口(interface)BlogService
来告诉spring BlogServiceImpl
是BlogService
的实现
public class BlogServiceImpl implements BlogService
并且我强烈建议您遵循包结构 As per spring docs那么您将不需要包含 @ComponentScan
来创建 Bean
。
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
关于java - 为什么我会收到 bean 'com.mypackage.service.blog.BlogService' 的 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48199428/