Spring Boot java.lang.IllegalArgumentException:不是托管类型

时间:2019-08-25 16:56:30

标签: spring spring-boot spring-data-jpa

我正在尝试运行一个非常简单的Spring Boot应用程序,但收到以下错误消息:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoController': Unsatisfied dependency expressed through field 'todoDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoDAO': Unsatisfied dependency expressed through field 'todoRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'todoRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.ecominer.model.Todo
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoDAO': Unsatisfied dependency expressed through field 'todoRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'todoRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.ecominer.model.Todo
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'todoRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.ecominer.model.Todo
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.ecominer.model.Todo

这是我的主要应用程序类代码:

package com.ecominer.network;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories("com.ecominer.repository") 
@EnableJpaAuditing
@ComponentScan({"com.ecominer.controller", "com.ecominer.dao", "com.ecominer.model"})
public class NetworkApplication {

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

}

这是Todo类:

package com.ecominer.model;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name="todos")
@EntityListeners(AuditingEntityListener.class)
public class Todo {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	protected Todo() {}
	
	private String label;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}
}

我尝试将相应的程序包名称添加到我的@ComponentScan批注中,并尝试将@Component注释添加到我的Todo类中,但没有一个起作用。

1 个答案:

答案 0 :(得分:1)

尝试将@EntityScan配置为主要类NetworkApplication

@EntityScan(basePackages = "com.ecominer.model")
public class NetworkApplication {
...
}