Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我没有得到bean';com.mypackage.service.blog.BlogService';_Java_Spring_Spring Mvc_Spring Boot_Autowired - Fatal编程技术网

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();
  }
My
com.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
,告诉spring
BlogServiceImpl
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
,告诉spring
BlogServiceImpl
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