我们可以在 spring 的实体对象上使用 @autowired 吗? [英] Can we use @autowired on an entity object in spring?

查看:20
本文介绍了我们可以在 spring 的实体对象上使用 @autowired 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Customer 的实体类,我在另一个类中使用这个实体对象来设置数据.当我在下面使用这个对象时

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
Customer customer

Spring 抱怨请在您的类中配置 bean.

Spring is complaining that please configure the bean in your classes.

我们可以对实体对象使用自动连接吗?

Can we use auto wiring with entity objects?

推荐答案

您只能自动装配那些生命周期由 Spring IoC 容器.

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

这些 bean 以 xml 形式定义,带有 </bean> 标签,或者一些特殊的注释,如 @Bean@Component@Service@Repository

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

另一方面,

简单来说,实体是一些你需要创建的java对象,根据你的业务逻辑自行更新,并在数据库中保存/更新/删除它们.Spring IoC 容器无法管理它们的生命周期.

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!

这篇关于我们可以在 spring 的实体对象上使用 @autowired 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆