[英]2 beans with same name but different package, how to autowire?
In my java project, I have 2 entities with same name but different package, also I have corresponding dao for these entities. 在我的java项目中,我有2个具有相同名称但不同包的实体,我对这些实体也有相应的dao。
Now because of 2 entities with same name, it was giving duplicate scan error, and so I added name attribute to these entities with their fully qualified name. 现在由于2个具有相同名称的实体,它给出了重复的扫描错误,因此我使用其完全限定名称向这些实体添加了name属性。
Ex: Entity(name="pckEntity) & Entity(name="pabEntity) 例如:实体(name =“pckEntity)&Entity(name =”pabEntity)
But now I their corresponding daos are not able to autowire, and I am getting the following error: 但是现在我对应的daos无法自动装配,我收到以下错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type...
Do I have to change anything in Dao also to support this "name" attribute change in the entity. 我是否还必须更改Dao中的任何内容以支持实体中的“名称”属性更改。
I am using Hibernate, JPA and Spring. 我正在使用Hibernate,JPA和Spring。
By default autowiring is done by type. 默认情况下,自动装配按类型完成。 So you can directly use
@Autowired
annotation as both Entity are different classes, make sure those are spring beans (here I mean those are managed by Spring). 所以你可以直接使用
@Autowired
注释,因为两个实体都是不同的类,确保它们是spring bean(这里我的意思是那些由Spring管理)。
@Autowired // nothing to specify, Spring automatically autowire the bean by checking type
private p.c.k.Entity entity;
@Autowired // nothing to specify, Spring automatically autowire the bean by checking type
private p.a.b.Entity entity1;
I also had this problem, and couldn't find any way to work around it other than to rename one of the classes. 我也有这个问题,除了重命名其中一个类之外,找不到任何解决方法。 Being in different packages should be enough, but it's not.
在不同的包装应该是足够的,但事实并非如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.