No qualifying bean of type is defined: expected single matching bean but found 2

Recently, in Spring's annotation, trying to get a bean has the error shown in the title.

NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean but found 2:sysRoleManage, sysRoleManageImpl 

 

I have 2 classes, 1 is an interface:

 

public interface SysRoleManager{
  //............
}

 The other is the implementation class of the interface:

 

 

@Compnent
public class SysRoleManagerImpl implements SysRoleManager{
  //............implement abstract method
}

 When trying to get the bean:

 

 

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext);

SysRoleManager manager = ctx.getBean(SysRoleManager.class);
System.out.println(manager);

 Got the error as shown in the title.

 

 

As a solution, the annotations on the implementation class should be more detailed and changed to:

 

@Service("sysRoleManager")
public class SysRoleManagerImpl implements SysRoleManager{
  //............implement abstract method
}

 This solves the problem, printing out:

 

com.xx.xxx.SysRoleManagerImpl@179e64

Indicates that the implementation class has been obtained.

Or you can get it directly through annotations without going through the Spring context:

@Autowire
@Qualifier("sysRoleManager ")
private SysRoleManager manager;

 

If there are multiple implementation classes that implement the same interface, you need to annotate different @Service("name") on each implementation class, and the name is a different value.

 

By the way, add an exception of hibernate: for example,

org.hibernate.HibernateException: identifier of an instance 

 

of org.cometd.hibernate.User altered from 12 to null。

that is because:

1) Do you configure the non-primary key field with a primary key generation strategy such as @NaturalId when annotating;

2) Whether the primary key is updated in a transaction, and the primary key cannot be updated.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802033&siteId=291194637