gpt4 book ai didi

java - Spring 启动: @Autowired remember and save the same object also for the next request

转载 作者:行者123 更新时间:2023-12-02 10:52:10 26 4
gpt4 key购买 nike

我正在开发一个 Spring Boot 应用程序,例如下面的一个 API:

@Autowired
GetUserIdByUserNameBase getUserIdByUserNameBase;

@PostMapping(value = "/UserName")
public GetUserIdByUserNameOutput getUserIdByUserName(@RequestBody String userName) {
GetUserIdByUserNameInput GetUserIdByUserNameInput = new GetUserIdByUserNameInput(userName);
getUserIdByUserNameBase.setGetUserIdByUserNameInput(GetUserIdByUserNameInput);
MsgStatus msgStatus = getUserIdByUserNameBase.execute();
if (msgStatus == MsgStatus.ERROR_MSG) {
LOG.error("UserRestController,getUserIdByUserName : Internal System Error");
}
return getUserIdByUserNameBase.getGetUserIdByUserNameOutput();
}

我的问题出现在具有相同 API 的第二个/下一个请求中(例如上面的一个)“GetUserIdByUserNameBase”仍然记住所有保存的数据第一击!.

public class GetUserIdByUserNameBase extends ServiceBase {

private final Logger LOG = LogManager.getLogger(GetUserIdByUserNameBase.class);
private GetUserIdByUserNameInput getUserIdByUserNameInput;
private GetUserIdByUserNameOutput getUserIdByUserNameOutput;
private ResultStatus resultStatus;
private String userName;
private TBUSER_INFO userInfo;

在我的例子中,“getUserIdByUserNameOutput”仍然保存以前的从第一次点击 API 开始的数据,直到我覆盖内容或将其标记为空。

1. GetUserIdByUserNameBase.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput );

  • GetUserIdByUserNameBase.getGetUserIdByUserNameOutput().setX(Null);
  • 但在这里我产生了一个问题,因为我为每个新的 API 请求创建了一个“GetUserIdByUserNameOutput”的新实例,并且对于系统来说效率不高且繁重......

    如果我总结一下我真正想知道的内容,我如何告诉@Autowired在 API 的每个新请求上弹跳/搁置对象,而不使用或覆盖它从第一个请求中记住的内容。?

    这里是我的完整代码:

    package com.usermanagementms.ims.servicesfacade.userservices;

    import javax.transaction.Transactional;

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import com.usermanagementms.ims.enums.MsgStatus;
    import com.usermanagementms.ims.repositories.userdao.TBUserInfoDao;
    import com.usermanagementms.ims.tables.user.TBUSER_INFO;
    import com.usermanagementms.ims.types.commontypes.ResultStatus;
    import com.usermanagementms.ims.types.getuseridbyusername.GetUserIdByUserNameInput;
    import com.usermanagementms.ims.types.getuseridbyusername.GetUserIdByUserNameOutput;
    import com.usermanagementms.ims.utils.ErrorMsg;
    import com.usermanagementms.ims.utils.ErrorsController;
    import com.usermanagementms.ims.utils.Utils;

    @Service
    public class GetUserIdByUserNameBase extends ServiceBase {

    private final Logger LOG = LogManager.getLogger(GetUserIdByUserNameBase.class);
    private GetUserIdByUserNameInput getUserIdByUserNameInput;
    private GetUserIdByUserNameOutput getUserIdByUserNameOutput;
    private ResultStatus resultStatus;
    private String userName;
    private TBUSER_INFO userInfo;

    @Autowired
    private TBUserInfoDao TBUserInfoDao;

    @Autowired
    private ErrorsController errorsController;

    @Transactional
    public TBUSER_INFO getUserIdByUserName(String username) {

    TBUSER_INFO userInfo;
    try {
    userInfo = TBUserInfoDao.findIdByUserName(username);
    if (userInfo == null) {
    LOG.warn("UserService,getUserIdByUserName : userInfo is empty");
    return null;
    }
    return userInfo;
    } catch (Exception e) {
    LOG.error("UserService,getUserIdByUserName : error in retrieving user from DB { " + e.getMessage() + "}");
    }
    return null;
    }

    @Override
    public MsgStatus inputMapping() {
    MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
    this.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput());
    if (getResultStatus() == null) {
    this.setResultStatus(new ResultStatus());
    }
    msgStatus = super.inputMapping();
    if (msgStatus == MsgStatus.ERROR_MSG || this.getUserIdByUserNameInput == null) {
    LOG.error("GetUserIdByUserNameBase,inpuMapping() : Internal System Error");
    this.getErrorsController().fillResultStatus(ErrorMsg.SYSTEM_ERROR, this.resultStatus);
    this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
    return MsgStatus.ERROR_MSG;
    }
    this.setUserName(getUserIdByUserNameInput.getUserNameX1());
    return MsgStatus.SUCCESS_MSG;
    }

    @Override
    public MsgStatus validate() {
    if (Utils.isNullorEmpty(userName)) {
    LOG.error("GetUserIdByUserNameBase,validate() : userName is null or empty");
    this.getErrorsController().fillResultStatus(ErrorMsg.REQUEST_BODY_EMPTY, this.resultStatus);
    this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
    return MsgStatus.ERROR_MSG;
    }
    return MsgStatus.SUCCESS_MSG;
    }

    @Override
    public MsgStatus executeImpl() {
    MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
    TBUSER_INFO userInfo = getUserIdByUserName(userName);
    this.setUserInfo(userInfo);
    if (userInfo == null) {
    msgStatus = this.getErrorsController().fillResultStatus(ErrorMsg.USER_NOT_FOUND, resultStatus);
    if (msgStatus == MsgStatus.ERROR_MSG) {
    this.getErrorsController().fillResultStatus(ErrorMsg.SYSTEM_ERROR, resultStatus);
    this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
    LOG.error("GetUserIdByUserNameBase,executeImpl() : Internal System Error");
    return MsgStatus.ERROR_MSG;
    }
    this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
    return MsgStatus.ERROR_MSG;
    }
    this.getErrorsController().fillResultStatus(ErrorMsg.OK, resultStatus);
    this.getGetUserIdByUserNameOutput().setResultStatus(resultStatus);
    return MsgStatus.SUCCESS_MSG;
    }

    @Override
    public MsgStatus outputMapping() {
    this.getGetUserIdByUserNameOutput().setUserId(this.getUserInfo().getUid());
    return MsgStatus.SUCCESS_MSG;
    }

    public GetUserIdByUserNameInput getGetUserIdByUserNameInput() {
    return getUserIdByUserNameInput;
    }

    public void setGetUserIdByUserNameInput(GetUserIdByUserNameInput getUserIdByUserNameInput) {
    this.getUserIdByUserNameInput = getUserIdByUserNameInput;
    }

    public GetUserIdByUserNameOutput getGetUserIdByUserNameOutput() {
    return getUserIdByUserNameOutput;
    }

    public void setGetUserIdByUserNameOutput(GetUserIdByUserNameOutput getUserIdByUserNameOutput) {
    this.getUserIdByUserNameOutput = getUserIdByUserNameOutput;
    }

    public String getUserName() {
    return userName;
    }

    public void setUserName(String userName) {
    this.userName = userName;
    }

    public ErrorsController getErrorsController() {
    return errorsController;
    }

    public void setErrorsController(ErrorsController errorsController) {
    this.errorsController = errorsController;
    }

    public ResultStatus getResultStatus() {
    return resultStatus;
    }

    public void setResultStatus(ResultStatus resultStatus) {
    this.resultStatus = resultStatus;
    }

    public TBUSER_INFO getUserInfo() {
    return userInfo;
    }

    public void setUserInfo(TBUSER_INFO userInfo) {
    this.userInfo = userInfo;
    }
    }

    public class GetUserIdByUserNameOutput {

    private Long UserId;
    private ResultStatus resultStatus;

    public GetUserIdByUserNameOutput() {

    }

    public GetUserIdByUserNameOutput(Long userId, ResultStatus resultStatus) {
    super();
    UserId = userId;
    this.resultStatus = resultStatus;
    }

    public GetUserIdByUserNameOutput(ResultStatus resultStatus) {
    this.resultStatus = resultStatus;
    }

    public Long getUserId() {
    return UserId;
    }

    public void setUserId(Long userId) {
    UserId = userId;
    }

    public ResultStatus getResultStatus() {
    return resultStatus;
    }

    public void setResultStatus(ResultStatus resultStatus) {
    this.resultStatus = resultStatus;
    }
    }

    如果你看看这个方法:

    public MsgStatus inputMapping() {
    MsgStatus msgStatus = MsgStatus.SUCCESS_MSG;
    this.setGetUserIdByUserNameOutput(new GetUserIdByUserNameOutput());

    每次对相同 API 的请求时,我都会开始创建“new GetUserIdByUserNameOutput()”! ,而且我想对于系统来说,在一段时间后创建大量 GetUserIdByUserNameOutput 实例并不高效且繁重......

    最佳答案

    在大多数情况下,Spring bean 不应该有状态。 bean 是可重用的类的单个实例。不要使用 setter !

    不要这样做:

    getUserIdByUserNameBase.setGetUserIdByUserNameInput(GetUserIdByUserNameInput);

    您需要在单个方法中进行计算,或者可能在 bean 内实例化一个新对象并返回新对象,例如

    return getUserIdByUserNameBase.getGetUserIdByUserName(GetUserIdByUserNameInput);

    或者类似的东西

    return getUserIdByUserNameBase.buildUsernameThing(GetUserIdByUserNameInput).execute();

    关于java - Spring 启动: @Autowired remember and save the same object also for the next request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086554/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com