【问题标题】:Error creating bean with name : Unsatisfied dependency expressed使用名称创建 bean 时出错:表示不满足的依赖关系
【发布时间】:2018-05-04 04:08:57
【问题描述】:

我在尝试启动我的应用程序时遇到此错误。我查看了许多类似的问题和主题,但似乎没有一个对我有帮助。

创建名为“databaseManager”的 bean 时出错:依赖关系不满足 通过字段“articleRepo”表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 'pl.dzejkobdevelopment.database.repositories.ArticleRepo' 可用: 预计至少有 1 个 bean 有资格作为 autowire 候选者。依赖 注释: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

@Repository
public interface ArticleRepo extends CrudRepository<Article, Long> {
}

还有……

@Service
public class DatabaseManager {

    @Autowired
    private ArticleRepo articleRepo;
    @Autowired
    private CommentRepo commentRepo;
    @Autowired
    private TagRepo tagRepo;
    @Autowired
    private UserRepo userRepo;

    public void addArticle(Article article){
        article.getTags().forEach(tag ->addTag(tag));
        articleRepo.save(article);
    }

    public List<Comment> findComments(User user){
        return commentRepo.findByCommentAuthor(user);
    }

    private void addTag(Tag tag){
        tagRepo.save(tag);
    }


}

还有……

@Configuration
//@ComponentScan(basePackages="pl.dzejkobdevelopment.database.repositories")
public class AppConfig {
    @Bean
    public WebsiteProporties websiteProporties(){
        return new WebsiteProporties();
    }
    @Bean
    public StorageProperties storageProporties(){ return new StorageProperties();}
    @Bean
    public DatabaseManager databaseManager(){ return new DatabaseManager();}

    }
}

取消注释 ComponentScan 没有帮助。

编辑ComponentScan 更改为 EnableJpaRepositories 会出现此错误:

创建名为“databaseManager”的 bean 时出错:通过字段“articleRepo”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“articleRepo”的 bean 时出错:设置 bean 时无法创建 [org.springframework.orm.jpa.SharedEntityManagerCreator] 类型的内部 bean '(inner bean)#14a1d6d'属性“实体管理器”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 '(inner bean)#14a1d6d' 的 bean 时出错:设置构造函数参数时无法解析对 bean 'entityManagerFactory' 的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

【问题讨论】:

    标签: java spring hibernate jpa spring-data


    【解决方案1】:

    尝试使用

    @EnableJpaRepositories("pl.dzejkobdevelopment.database.repositories")
    

    而不是 ComponentScan。

    【讨论】:

    • 太好了——最初的问题已经解决了。现在你必须配置 JPA。
    • 类似这样的东西:@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); // 设置属性返回 emf; }
    • 嗯,好的,但我使用的是 Spring Boot。我以为它已经配置好了。我在 application.configuration 文件中有所有数据库属性(用户名、密码等)。它可以工作,因为我可以使用 CommandLineRunner 添加实体
    • 如果你想使用 SpringBoot,你需要告诉 Spring 使用它 :) 你可能想为你的配置类添加更多注释:@SpringBootApplication 或至少 @EnableAutoConfiguration
    猜你喜欢
    • 2023-03-25
    • 2023-03-12
    • 2018-10-20
    • 2019-02-22
    • 2019-10-11
    • 2021-07-23
    • 2020-11-25
    • 2017-05-15
    • 2018-03-19
    相关资源
    最近更新 更多