Java 为什么我没有得到bean';com.mypackage.service.blog.BlogService';
在春季,当我尝试从我的Java 为什么我没有得到bean';com.mypackage.service.blog.BlogService';,java,spring,spring-mvc,spring-boot,autowired,Java,Spring,Spring Mvc,Spring Boot,Autowired,在春季,当我尝试从我的BlogController执行操作时,我正在尝试解决一个未解决的Bean异常: @Autowired BlogService blogService; @RestController @RequestMapping("/blogs") public class BlogController { @Autowired private BlogService blogService; @PostMapping @ResponseStatus(HttpSt
BlogController
执行操作时,我正在尝试解决一个未解决的Bean异常:
@Autowired BlogService blogService;
@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();
}
- 我正在使用
org.springframework.stereotype.Service
Service注释
- 我的
ApiApplication
应用程序类用@ComponentScan(“com.mypackage”)
注释
- 服务实现是带有
@service
的注释,位于com.mypackage.service.blog.BlogService“
- 该服务不能是
自动连接的
,但它是由该服务使用的@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
:
@Autowired BlogService blogService;
@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();
}
Mycom.mypackage.service.blog.BlogService
class:
public interface BlogService extends CrudService<Blog, Long> {
}
@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)}
我应该使用一些特定的类路径进行调试,而另一个类路径进行信息处理吗?我在当前的调试日志中没有看到服务创建和类路径。您可以使用下面的类来查看在上下文中创建了哪些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));
}
}
您可以使用下面的类来查看在上下文中创建了哪些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));
}
}
点#1
此处不需要@ComponentScan
,只需将其从主应用程序类(即ApiApplication
)中删除即可
点#2
正如我们所看到的,BlogServiceImpl
没有实现BlogService
,这意味着没有具体的BlogService
实现,因此无法创建Bean
您需要实现BlogServiceImpl
接口BlogService
,告诉springBlogServiceImpl
是BlogService
的实现
public class BlogServiceImpl implements BlogService
而且我强烈建议您遵循包结构,这样您就不需要包含@ComponentScan
来创建Bean
s
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
点#1
此处不需要@ComponentScan
,只需将其从主应用程序类(即ApiApplication
)中删除即可
点#2
正如我们所看到的,BlogServiceImpl
没有实现BlogService
,这意味着没有具体的BlogService
实现,因此无法创建Bean
您需要实现BlogServiceImpl
接口BlogService
,告诉springBlogServiceImpl
是BlogService
的实现
public class BlogServiceImpl implements BlogService
而且我强烈建议您遵循包结构,这样您就不需要包含@ComponentScan
来创建Bean
s
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
我提供的信息不足以帮助您,请向我们展示您的ApiApplication类,包括注释。@MinjunYu我刚刚分享了它,您看到了有用的东西吗?为了更明确,我添加了两次扫描。@SpringBootApplication应该可以工作,因为您的ApiApplication类位于包的根目录下。添加另一个componentScan是非常有用的冗余。您的服务实现位于何处。@ComponentScan
不是必需的,因为它是@springbootapplication
的一部分。还要添加其他类(控制器和服务)。还要添加其他类(控制器和服务)
:你是什么意思?提供的信息不足以帮助你,请向我们展示你的ApiApplication类,包括注释。@MinjunYu我刚刚分享了它,你看到有用的东西了吗?我添加了两次扫描以更加明确。@SpringBootApplication应该可以工作,因为你的ApiApplication类位于包的根目录下。添加调用另一个组件扫描是多余的。您的服务实现位于何处。@componentScan
在这里不是必需的,因为它是@springbootapplication
的一部分。还要添加其他类(控制器和服务)。还要添加其他类(控制器和服务)
:你是什么意思?我可以在beanDefinitionNames
中看到有blogsrepository
和blogServiceImpl
,那么它为什么没有被创建呢?我可以在beanDefinitionNames
中看到有blogsrepository
和blogServiceImpl
,那么它为什么没有被创建呢?我想这就是我的答案如@antontupy所述,说明不+@SpringBootApplication应该已经足够了。显然beanDefinitionsNames包含它们,但由于某种原因,如果我决定不使用此结构,则仍然会出现NoSuchBeanDefinitionException
。这是因为我的项目由使用核心模块和插件的应用程序组成。ADDON在com.example.myproject.samducedon
下有自己的名称空间,它们也有可供projet使用的域/服务/控制器
。这些插件的复杂性可能需要在每个文件夹中创建一个子文件夹。您链接的Spring文档仅说明了在构建简单应用程序时该如何操作Application.@BigDong我刚刚在我的机器上尝试了你的项目。我刚刚删除了@ComponentScan({“com.mypackage”,“com.mypackage.service.blog”})
从主应用程序中,它成功了。请也这样做,并让我知道它是否也对您有效。然后我将在答案中添加我的推理。我已删除@ComponentScan
,它说com.mypackage.controller.blog.BlogController中的字段blogService需要一个类型为'com.mypackage.service.common.blogService'的bean在中找不到。
(请注意,我在common
becau中移动了BlogService