在春天,当我尝试从我的BlogController
中执行时,我正在尝试解决一个Unresolved Bean Exception
:
@Autowired BlogService blogService;
- 我正在使用
org.springframework.stereotype.Service
服务注释。 - 我的
ApiApplication
应用程序类用@ComponentScan("com.mypackage")
注释。 - 服务实现是带有
@Service
注释的,并且处于com.mypackage.service.blog.BlogService"
- 该服务无法
Autowired
,但它@Repository
由服务使用,并且位于com.mypackage.repository.blog.BlogRepository
可由控制器导入。
我的应用程序类如下所示:
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 日志中看不到服务创建和类路径。
> 点#1
这里不需要@ComponentScan
,只需将其从您的主应用程序类中删除即可,即 ApiApplication
,它会起作用。
第 #2
点
正如我们所看到的BlogServiceImpl
没有实现BlogService
这意味着没有具体的BlogService
实现,因此无法创建Bean
。
你需要实现BlogServiceImpl
接口BlogService
告诉春天BlogServiceImpl
是BlogService
的实现
public class BlogServiceImpl implements BlogService
我强烈建议您遵循包结构 根据 spring 文档,您无需包含@ComponentScan
即可Bean
创建。
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
您可以使用下面的类来查看在上下文中创建的 bean。哎呀,这会有所帮助。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
@Component
class BeansLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(BeansLogger.class);
private final ApplicationContext applicationContext;
@Autowired
public BeansLogger(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@PostConstruct
public void init() {
final String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.sort(beanDefinitionNames);
LOGGER.debug("Registered beans: {}", Arrays.asList(beanDefinitionNames));
}
}