2 beans with the same name but with a different package like autwire?

In my java project I have two objects with the same name but with a different package, also I have a corresponding dao for these objects.

Now, due to two entities with the same name, it was throwing a duplicate scan error, so I added a name attribute to those entities with their fully qualified names.

Ex: Entity (name = "pckEntity) and Entity (name =" pabEntity)

But now I their respective daos cannot autwire and I get the following error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type...

      

I need to change something in Dao to support this change to the "name" attribute in the entity.

I am using Hibernate, JPA and Spring.

+3


source to share


3 answers


I think you can use the annotation @Qualifier

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

      



Received from here

0


source


By default, autoinstall is performed by type. This way you can use the annotation directly @Autowired

, since both objects are different classes, make sure they are spring beans (here I mean they are managed by 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;

      

0


source


I also had this problem and couldn't find a way to work around it other than to rename one of the classes. Being in different packages should be enough, but it isn't.

0


source







All Articles