spring经常使用接口之InitializingBean

spring经常使用接口简介

InitializingBean接口

  • 做用:实现InitializingBean接口的bean,在初始化bean时都会执行afterPropertiesSet方法 afterPropertiesSespring

  • InitializingBean接口源码以下:api

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
复制代码
  • 使用实例:策略模式+InitializingBean
  • 业务场景:调用登陆接口后,须要根据接口返回的不一样的错误码,作出不一样响应
  • 实现思路:将错误码和场景实现类的映射关系,在bean初始化时经过afterPropertiesSet初试化至Map中(仿照spring初始化beandefinition),调用时经过调用接口返回的错误码get到相应的实现类,InitializingBean在此场景中起到初始化map的做用
/**
 * 登陆场景抽象类
 *
 * @author xiantao.wu
 * @create 2019/5/1711:32
 **/
public abstract class AbstractScene implements InitializingBean {
    private static final Map<Integer, AbstractScene> SCENE_MAP = new ConcurrentHashMap<>();
    //初始化Map
    protected void register(Integer sceneType, AbstractScene scene) {
        SCENE_MAP.put(sceneType, scene);
    }


    public static AbstractScene getScene(Integer sceneType) {
        if (sceneType == null) {
            throw 业务异常
        }
        return SCENE_MAP.get(sceneType);
    }


    //调用统一入口
    public static AppLoginResponse checkLoginResponse(LoginResponse response) {
        Integer sceneType=response.getCode;
        AbstractScene scene = getScene(sceneType);
        if (scene == null) {
           throw 业务异常
        }

        return scene.check(apiResponse);
    }

    //登陆check项抽象方法
    public abstract AppLoginResponse check(ApiResponse<TokenPersonLoginResult> apiResponse);

}
复制代码

具体实现类:bash

/**
 * 场景一:帐号存在风险
 *
 * @author xiantao.wu
 * @create 2019/5/1711:35
 **/
@Service
public class RiskAccountScene extends AbstractScene {
    @Autowired
    private PersonService personService;

    @Override
    public void afterPropertiesSet() throws Exception {
        register(Errors.RISK_ACCOUNT.getErrorCode(), this);
    }

    @Override
    public AppLoginResponse check(ApiResponse<TokenPersonLoginResult> apiResponse) {
        //TODO 具体实现逻辑
    }
}

/**
 * 场景二:须要验证手机
 *
 * @author xiantao.wu
 * @create 2019/5/1711:35
 **/
@Service
public class NeedVerifyMobileScene extends AbstractScene {
    @Autowired
    private PersonService personService;

    @Override
    public void afterPropertiesSet() throws Exception {
        register(Errors.NEED_VERIFY_MOBILE.getErrorCode(), this);
    }

    @Override
    public AppLoginResponse check(ApiResponse<TokenPersonLoginResult> apiResponse) {
        //TODO 具体实现逻辑
    }
}


登陆调用

 /**
     * 登陆
     */
    @ApiOperation(value = "登陆", tags = "PASSPORT")
    @PostMapping(value = "/login")
    public ApiResponse<AppLoginResponse> login(@RequestBody AppLoginRequest appLoginRequest) {
        LoginResponse response = loginService.login(appLoginRequest);
        return ApiResponse.success(AbstractScene.checkLoginResponse(response));
    }

复制代码
相关文章
相关标签/搜索