如何解决启动ApplicationContext时出错

时间:2019-07-07 06:18:07

标签: java spring

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-07 12:52:59.026 ERROR 10816 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authorDao in com.Jpa.Library.libraryItem.LibraryItemApplication required a bean of type 'com.Jpa.Library.libraryItemDao.AuthorDao' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.Jpa.Library.libraryItemDao.AuthorDao' in your configuration.

这是我的代码:

package com.Jpa.Library.libraryItem;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.Jpa.Library.libraryItemDao.AuthorDao;
import com.Jpa.Library.libraryItemDao.LibraryItemDao;
import com.Jpa.Library.libraryItemDao.RackDao;
import com.Jpa.Library.libraryItemDao.RackItemDao;
import com.Jpa.Library.libraryItemEntity.LibraryItem;

@SpringBootApplication
public class LibraryItemApplication implements ApplicationRunner{

    public static void main(String[] args) {
        SpringApplication.run(LibraryItemApplication.class, args);
    }

    @Autowired
    private AuthorDao authorDao;

    @Autowired
    private LibraryItemDao libraryItemDao;

    @Autowired
    private RackDao rackDao;

    @Autowired
    private RackItemDao rackItemDao;

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
    LibraryItem libraryitem = libraryItemDao.findByTitle("Fifty Shades of Gray");
    System.out.println(libraryitem);
    }
}

4 个答案:

答案 0 :(得分:1)

A)仔细阅读错误消息

B)“考虑在您的配置中定义类型为'com.Jpa.Library.libraryItemDao.AuthorDao'的bean。”

答案 1 :(得分:0)

<@Steve提出的问题>

答案 2 :(得分:0)

请在下面解决以下两个查询以获取解决方案:

  1. 您是否用AuthorDao注释了@Repository类?

  2. 您是否在@ComponentScan applicationContext.xml 中指定了基本程序包名称?例如:@ComponentScan("com.Jpa.Library") (用于基于注释的配置)<context:component-scan base-package="com.Jpa.Library" /> (用于基于XML的配置)

希望这可以解决您的问题。 :)

答案 3 :(得分:0)

  1. 由于是Spring Boot代码,由于包含@SpringBootApplication批注,因此将自动完成组件扫描。

  2. 扫描时将检查所有使用@Component注释注释的组件

因此,请确保您的bean具有该注释。您也可以通过从application.properties启用调试来检查此扫描。