简体   繁体   English

2个同名但不同的bean,如何自动装配?

[英]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。

I think you can use the @Qualifier annotation 我想你可以使用@Qualifier注释

@Autowired
@Qualifier("p.c.k.Entity")
private Entity entity;

Got from here 来自这里

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.

相关问题 是否可以在没有@qualifier 和@resource 的情况下自动装配相同 class 的两个不同 bean? 如果是,如何? - Is it possible to Autowire two different beans of the same class without @qualifier and @resource? If yes, how? 如何动态创建多个相同类型的bean然后收集/自动装配它们 - how to dynamically create multiple beans of same type then gather/autowire them 在春季,如何定义两个具有相同名称和不同类的Bean - In Spring, How to Define Two Beans, with Same Name and Different Class 具有相同类名(不同包)的两个Spring Service Bean即使使用限定符也会抛出Error - Two Spring Service Beans with same Class Name (different package) are throwing Error even with qualifier 在不同上下文中具有相同名称的两个bean - Two beans with same name in different contexts 带有Spring Bean的MDB不会自动将另一个软件包中的Bean接线 - MDB with spring beans doesn't autowire beans from another package 如何在不使用 ApplicationContext 按名称获取 Bean 的情况下在非 Spring 对象中自动装配 - How to autowire in non Spring objects without using the ApplicationContext to get Beans by name 自动装配两个实现相同接口的 bean - 如何将默认 bean 设置为自动装配? - Autowiring two beans implementing same interface - how to set default bean to autowire? 如何使用注释动态自动装配 Spring 中的 bean? - How to dynamically autowire beans in Spring with annotations? 如何在NoRepositoryBean的实现内部自动装配bean - How to autowire beans inside the implementation of a NoRepositoryBean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM