0
votes

I'm trying to run a very simple Spring Boot application, but I get the following error messages:

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

Here is my main application class code:

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);
	}

}

and here is the Todo class:

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;
	}
}

I tried to add the corresponding package name to my @ComponentScan annotation and I tried adding the @Component annotation to my Todo class, none of which worked.

1
Todo is not a Spring bean. It's a JPA entity. So ComponentScan is irrelevant. What you need is... EntityScan. You wouldn't need all those Scan annotations if you respected the recommended layout and put your main class in the com.ecominer package.JB Nizet

1 Answers

1
votes

Try to configure @EntityScan to the main class NetworkApplication

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