2016-12-31 49 views
0

下面是跟蹤:錯誤創建名稱爲豆「XXX:不滿意依賴通過現場表示 'XXX'

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class modele.Test

...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class modele.Test

...

Caused by: java.lang.IllegalArgumentException: Not a managed type: class modele.Test

根據我的理解,根錯誤是Not a managed type: class modele.Test,這與測試未被識別爲實體有關嗎?

這裏是我的項目:

架構:http://imgur.com/a/2xsI4

Application.java

@SpringBootApplication 
@ComponentScan("boot") 
@ComponentScan("dao") 
@ComponentScan("modele") 
@EnableJpaRepositories("dao") 
public class Application { 

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

} 

TestDAO.java

@Transactional 
public interface TestDAO extends CrudRepository<Test, Long > { 

    /** 
    * This method will find an User instance in the database by its email. 
    * Note that this method is not implemented and its working code will be 
    * automagically generated from its signature by Spring Data JPA. 
    */ 
    public Test findByEmail(String email); 

} 

Test.java

@Entity 
@Table(name = "test") 
public class Test { 

    // An autogenerated id (unique for each user in the db) 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @NotNull 
    private String email; 

    @NotNull 
    private String name; 

    // Public methods 

    public Test() { 
    } 

    public Test(long id) { 
     this.id = id; 
    } 

    public Test(String email, String name) { 
     this.email = email; 
     this.name = name; 
    } 
//setters and getters 

我會很感激的任何幫助。謝謝!

+0

請注意,如果將應用程序放在「dao」,「modele」和「boot」的父包中,那麼這4個最後的註釋是無用的。 Spring Boot將基於此自動應用合理的默認值。 –

+0

是的,但我的代碼並非如此。儘管我現在已經改變了! – Chuck

回答

1

根據您目前的設置,你需要添加

@EntityScan("modele") 

Test是不是真的每說一個Spring bean,這是一個JPA實體@ComponentScan的搜索結果@Configuration@Component@Service@Repository@Controller@RestController@EntityScan將查找實體。

您可以參閱:Difference between @EntityScan and @ComponentScan

您的配置會容易得多,如果你想移動:

  • Application.java在你的包的根:com.domain.project;
  • 您的知識庫在com.domain.project.dao之下;
  • 您的實體根據com.domain.project.domain

然後,你就不需要@EntityScan@ComponentScan@EnableJpaRepositories,SpringBoot只會皮卡一切com.domain.project找到。*

+0

謝謝,修改了架構,正如您在其中解釋的那樣。 – Chuck

相關問題