找不到名为“entityManagerFactory”的bean

Posted

技术标签:

【中文标题】找不到名为“entityManagerFactory”的bean【英文标题】:bean named 'entityManagerFactory' could not be found 【发布时间】:2019-05-24 05:39:04 【问题描述】:

我的应用程序无法启动。它由 Spring Boot + JPA + SQLite 组成。但是 Spring Boot 和 mysql 工作正常。我不明白为什么会这样。

错误是

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.15.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>

代码:

@Entity
public class Contact implements Serializable 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int ContactID;
    String Number;
    String textMessage;
    String DownloadID;
    long ContactFlags;

    public int getContactID() 
        return ContactID;
    

    public void setContactID(int contactID) 
        ContactID = contactID;
    

    public String getNumber() 
        return Number;
    

    public void setNumber(String number) 
        Number = number;
    

    public String getTextMessage() 
        return textMessage;
    

    public void setTextMessage(String textMessage) 
        this.textMessage = textMessage;
    

    public String getDownloadID() 
        return DownloadID;
    

    public void setDownloadID(String downloadID) 
        DownloadID = downloadID;
    

    public long getContactFlags() 
        return ContactFlags;
    

    public void setContactFlags(long contactFlags) 
        ContactFlags = contactFlags;
    


public interface ContactRepository extends CrudRepository<Contact,Integer> 


@Configuration
public class DBConfig 
    @Bean
    public DataSource dataSource()
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.sqlite.JDBC");
        dataSourceBuilder.url("jdbc:sqlite::/home/vivien/viber.db");
        return dataSourceBuilder.build();
    


public class SQLiteDialect extends Dialect     
    SQLiteDialect()
        registerColumnType(Types.BIT, "integer");
        registerColumnType(Types.TINYINT, "tinyint");
        registerColumnType(Types.SMALLINT, "smallint");
        registerColumnType(Types.INTEGER, "integer");
        registerColumnType(Types.BIGINT, "bigint");
        registerColumnType(Types.FLOAT, "float");
        registerColumnType(Types.REAL, "real");
        registerColumnType(Types.DOUBLE, "double");
        registerColumnType(Types.NUMERIC, "numeric");
        registerColumnType(Types.DECIMAL, "decimal");
        registerColumnType(Types.CHAR, "char");
        registerColumnType(Types.VARCHAR, "varchar");
        registerColumnType(Types.LONGVARCHAR, "longvarchar");
        registerColumnType(Types.DATE, "date");
        registerColumnType(Types.TIME, "time");
        registerColumnType(Types.TIMESTAMP, "timestamp");
        registerColumnType(Types.BINARY, "blob");
        registerColumnType(Types.VARBINARY, "blob");
        registerColumnType(Types.LONGVARBINARY, "blob");
        // registerColumnType(Types.NULL, "null");
        registerColumnType(Types.BLOB, "blob");
        registerColumnType(Types.CLOB, "clob");
        registerColumnType(Types.BOOLEAN, "integer");
        registerFunction( "concat", new VarArgsSQLFunction(StringType.INSTANCE, "", "||", "") );
        registerFunction( "mod", new SQLFunctionTemplate( StringType.INSTANCE, "?1 % ?2" ) );
        registerFunction( "substr", new StandardSQLFunction("substr", StringType.INSTANCE) );
        registerFunction( "substring", new StandardSQLFunction( "substr", StringType.INSTANCE) );
    
    public boolean supportsIdentityColumns() 
        return true;
    

    spring.datasource.url=jdbc:sqlite::/home/vivien/viber.db
    spring.datasource.driver-class-name = org.sqlite.JDBC
    spring.datasource.username=
    spring.datasource.password=
    spring.jpa.database-platform=com.example.demo.SQLiteDialect
    spring.jpa.show-sql=true

    @RestController
public class MyController 
     @Autowired
    ContactRepository contactRepository;

    @GetMapping("/hello")
    public String Hello()
        return "Hello Sqlite";
    

    @GetMapping("/all")
    public Iterable<Contact> getAll()
        return contactRepository.findAll();
    

错误信息:

启动 ApplicationContext 时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。

2018-12-25 17:35:44.340 错误 25591 --- [主要]

o.s.b.d.LoggingFailureAnalysisReporter:

****************************************** 应用程序启动失败 ************************************************

说明:

com.example.demo.controller.MyController 中的字段 contactRepository 需要一个名为“entityManagerFactory”的 bean,但无法找到。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

【问题讨论】:

你能在 MyController 类上发布代码吗? 我重新格式化了您的问题并将“标题”移到了问题正文中。标题实际上仅用于非常简短的描述,例如错误消息的关键部分。 @JonathanJohx 我添加了 @trincot 谢谢! 你找到解决办法了吗? 【参考方案1】:

这里的问题是您在 MyController 类中自动装配了一个 EntityManagerFactory。 EntityManagerFactory 无法直接被@Autowired 或@Resource 注入

如果您需要对 EntityMananger 进行依赖注入,请使用

@PersistenceContext
private EntityManager entityManager;

【讨论】:

以上是关于找不到名为“entityManagerFactory”的bean的主要内容,如果未能解决你的问题,请参考以下文章

找不到与名为 '' 的控制器匹配的类型

找不到名为游标的模块

关于如何修复错误“找不到名为 viewcontroller 的类的任何信息”的建议

找不到名为“entityManagerFactory”的bean

找不到名为“interlockedexchange”的入口点

django wsgi 错误找不到名为站点名称的模块