其他分享
首页 > 其他分享> > 对于@Primary注解的使用

对于@Primary注解的使用

作者:互联网

当一个接口存在多个实现的时候会报org.springframework.beans.factory.NoUniqueBeanDefinitionException类似的异常信息,项目中碰到引用别人事先写好的框架,但是对于其中个别实现并不是很需要,因此可以通过使用@Primary注解进行处理

比如处理登录失败时

@Component
public class OtherAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
public HcAuthenticationFailureHandler() {
}

public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
if (exception instanceof SessionAuthenticationException) {
ResponseUtil.out(response, HttpStatus.UNAUTHORIZED, AjaxMessage.error("该账户登录数量已达上限,请先下线别处的登录!"));
} else {
ResponseUtil.out(response, HttpStatus.UNAUTHORIZED, AjaxMessage.error(exception.getMessage()));
}

}
}


我需要对返回的异常再进一步处理,@Primary指优先使用本处理器
@Primary
@Component
public class AuthenticationFailureHandler extends HcAuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
if (exception instanceof SessionAuthenticationException) {
ResponseUtil.out(response, HttpStatus.UNAUTHORIZED, AjaxMessage.error("该账户登录数量已达上限,请先下线别处的登录!"));
} else {
if (exception instanceof ValidateLoginException && exception.getMessage().equals("账户不存在")) {
ResponseUtil.out(response, HttpStatus.UNAUTHORIZED, AjaxMessage.error("用户名或密码错误"));
} else {
ResponseUtil.out(response, HttpStatus.UNAUTHORIZED, AjaxMessage.error(exception.getMessage()));
}
}

}
}

标签:exception,ResponseUtil,UNAUTHORIZED,AjaxMessage,Primary,使用,注解,response,out
来源: https://www.cnblogs.com/cowardy/p/15789822.html