我的应用程序无法启动。它由Spring Boot + JPA + SQLite组成。但是Spring Boot和MySQL运行正常。我不明白为什么会这样。
错误是
找不到名为“ entityManagerFactory”的bean
<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 --- [main]
o.s.b.d.LoggingFailureAnalysisReporter:
***************************************
应用程序无法启动
*******************************************说明:
com.example.demo.controller.MyController中的field contactRepository字段需要一个名为'entityManagerFactory'的bean,但找不到。
注入点具有以下注释:
-@ org.springframework.beans.factory.annotation.Autowired(required = true)
答案 0 :(得分:0)
这里的问题是您将MyController类中的EntityManagerFactory自动接线。 EntityManagerFactory无法通过@Autowired或@Resource
直接注入如果您需要EntityMananger的依赖项注入,请使用
@PersistenceContext
private EntityManager entityManager;