【问题标题】:Springboot and IDEA error: Could not autowire. No beans of 'EntityLinks' type foundSpringboot 和 IDEA 错误:无法自动装配。找不到“EntityLinks”类型的 bean
【发布时间】:2021-01-17 16:08:43
【问题描述】:

我正在关注 Spring in action 第 5 版来学习 Springboot。在第 6 章时,我发现我的 IDEA IDE 似乎存在 bean org.springframework.hateoas.server.EntityLinks 的错误。

package tech.enigma.web.api;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.enigma.Taco;
import tech.enigma.data.TacoRepository;


@RestController
@RequestMapping(path = "/design", produces = "application/json")
@CrossOrigin(origins = "*")
public class DesignTacoController
{
    private TacoRepository tacoRepo;
    private EntityLinks entityLinks;

    public DesignTacoController(TacoRepository tacoRepo, EntityLinks entitylinks)
    {
        this.tacoRepo = tacoRepo;
        this.entityLinks = entitylinks;
    }

    @GetMapping("/recent")
    public Iterable<Taco> recentTacos()
    {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createAt").descending());

        return tacoRepo.findAll(page).getContent();
    }
}

在公共 DesignTacoController(TacoRepository tacoRepo, EntityLinks entitylinks) 构造函数中,IDEA 给出错误“无法自动装配。找不到 'EntityLinks' 类型的 bean。”我可以编译并运行我的程序,尽管我不确定它是否能正常工作。 其他豆子一切正常。 这只是IDEA的一个错误还是我有什么问题?

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    这是known issue。由于对自动配置资源的错误扫描,IntelliJ 不会总是拾取所有可用的 bean。 重要的是 Spring 运行时。如果它没有导致错误,你就可以开始了。

    【讨论】:

    • 谢谢,我想知道为什么这很难解决,因为我在三年前看到类似的堆栈溢出问题
    • IntelliJ 需要索引可用的 bean。根据特定的注释,很难知道范围内的内容。如果一个库甚至更改了这些注释,它就会中断。如果您不知道注释,它将中断。如果它基于类路径上的库,而 intellij 还不知道它会中断。它需要在不启动 spring 运行时的情况下模拟所有这些。
    【解决方案2】:

    @Autowired 注释丢失。无论是构造函数注入还是setter注入都可以。

    package tech.enigma.web.api;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Sort;
    import org.springframework.hateoas.server.EntityLinks;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import tech.enigma.Taco;
    import tech.enigma.data.TacoRepository;
    
    
    @RestController
    @RequestMapping(path = "/design", produces = "application/json")
    @CrossOrigin(origins = "*")
    public class DesignTacoController
    {
        @Autowired
        private TacoRepository tacoRepo;
        @Autowired
        private EntityLinks entityLinks;
    
       
    
        @GetMapping("/recent")
        public Iterable<Taco> recentTacos()
        {
            PageRequest page = PageRequest.of(
                    0, 12, Sort.by("createAt").descending());
    
            return tacoRepo.findAll(page).getContent();
        }
    }
    

    【讨论】:

    • 如果只有一个构造函数,自 Spring 4.3 起,Autowired 是可选的
    • 三种方法我都试过了,结果都一样。
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 2019-09-16
    • 2022-01-09
    • 2022-11-08
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多