Can we use @autowired on an entity object in spring?

后端 未结 4 998
别跟我提以往
别跟我提以往 2021-01-22 01:18

I have a entity class called Customer, I am using this entity object in another class to set the data. When I use this object below like

@Autowired
         


        
4条回答
  •  面向向阳花
    2021-01-22 01:55

    If you mean JPAs @Entity-annotation, Spring is simply telling you, that there isn't a bean in its context. On startup/runtime classes in the application will be scanned and each class annotated with spring annotations like @Component, @Service etc. will be instantiated as beans and put into a global context (Spring applicationcontext). This context is then used to lookup and inject those beans into other beans when @Autowired is found during scanning.

    Opposed to this, @Entity is used during the creation of the Persistence-Context of JPA (as far as I remember) which isn't aware of Spring and it's context.

    Most of the solutions to make both contexts aware of each other a mostly a little bit hacky.

提交回复
热议问题