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

后端 未结 4 1036
别跟我提以往
别跟我提以往 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:50

    You can only autowire only those beans whose life-cycle are managed by Spring IoC container.

    These beans are defined in xml form with tag, or with some special annotations like @Bean, @Component, @Service, @Repository etc.

    On the other hand,

    in simple terms, entities are some java objects that you will need to create, update by yourself according to your business logic and save/update/remove them in/from DB. Their life-cycle cannot be managed by Spring IoC container.

    So, you should never feel like you need to autowire an entity if you are doing it right!

提交回复
热议问题