Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导API-如何确保没有并发问题_Java_Spring_Multithreading_Spring Boot - Fatal编程技术网

Java Spring引导API-如何确保没有并发问题

Java Spring引导API-如何确保没有并发问题,java,spring,multithreading,spring-boot,Java,Spring,Multithreading,Spring Boot,我仍在学习Java/spring,我认为我正在进步。现在我可以构建一个RESTAPI了,但是我不知道如何确保没有并发问题。我读过很多关于使API无状态或使POJO不可变的主题,但我确信在我下面的例子中,我是否需要这样做。如果我这样做了,我实际上不确定我的代码如何通过使POJO中的所有内容最终化来运行 如果有人能帮我在这里学习,我将非常感激。谢谢你抽出时间 下面我有一个名为User的POJO: @Getter @Setter @Document(collection = "UserPro

我仍在学习Java/spring,我认为我正在进步。现在我可以构建一个RESTAPI了,但是我不知道如何确保没有并发问题。我读过很多关于使API无状态或使POJO不可变的主题,但我确信在我下面的例子中,我是否需要这样做。如果我这样做了,我实际上不确定我的代码如何通过使POJO中的所有内容最终化来运行

如果有人能帮我在这里学习,我将非常感激。谢谢你抽出时间

下面我有一个名为User的POJO:

@Getter
@Setter
@Document(collection = "UserProfiles")
public class User {
    @Id
    @JsonIgnore
    private String _id;

    @JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
    private String userId;

    @JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
    private String name;

    @JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
    private String displayName;

    @DBRef
    @JsonView({ProfileViews.Full.class})
    private UserInterests personalInterests;

    @DBRef
    @JsonIgnore
    private ProfileFollows profileFollowDetails;

}

我的用户存储库/DALImpl:

@Repository
public class UserDALImpl implements UserDal {

    private final MongoTemplate mongoTemplate;

    @Autowired
    public UserDALImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

  @Override
    public User save(User user) {
        return mongoTemplate.save(user);
    }

最后,我的控制器:

@RestController
@RequestMapping("/profile")
public class CreateProfileController {

    @Autowired
    public CreateProfileController() {
    }

    @Autowired
    UserService userService;

    @ApiOperation(value = "Allows for the creation of a user Profile")
    @PostMapping("/create")
    public User createUserProfile(@RequestParam(name = "userId") String userId,
                                  @RequestParam(name = "displayName", required = true, defaultValue = "AnonymousDev") String displayName) {

        if (userId.equals("")) throw new BadRequestException("UserId cannot be blank");

        if (userService.checkIfUserIdExits(userId)) {
            throw new UserAlreadyExistsException("Unable to create user with Id { " + userId + " }, the " +
                    "userId already exists");
        }
        return userService.createNewUserAccount(userId, displayName);
    }
}

如果您没有使用任何并行操作,或者您使用的任何API/库都没有对您的对象进行并行操作,那么并发问题就不会成为问题。在其他情况下,您可以遵循一些同步技术。请注意,最终字段是线程安全的(并不总是),因为您无法为这些字段设置值。如果您根据需要设置值,则它不是final。即使您的对象声明为final,您也可以同时更新其值。Final只是防止更改对象引用。因此,将字段设置为final并不是处理并发问题的唯一方法。应同步对常用访问对象的更新。您可以搜索一下同步技术。非常感谢您花时间@GauthamM,当您说“如果您没有使用任何并行操作”时,您的意思是像多线程更改同一对象一样吗?如果我理解正确的话,情况就不会是这样(正确的??:-),因为createnewprofile方法实例化了它自己的新用户对象来修改,我在一般情况下是这么说的。不特定于所讨论的代码
public interface UserDal {
    /**
     * Method to check if a user exists with a given user Id
     * @param Id -- Id of user to look up where id is a string
     * @return
     */
    Boolean existsById(String Id);

    /**
     * Method to save a user to the DB
     * @param user -- User object to save to the DB
     * @return
     */
    User save(User user);
}
@Repository
public class UserDALImpl implements UserDal {

    private final MongoTemplate mongoTemplate;

    @Autowired
    public UserDALImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

  @Override
    public User save(User user) {
        return mongoTemplate.save(user);
    }

@RestController
@RequestMapping("/profile")
public class CreateProfileController {

    @Autowired
    public CreateProfileController() {
    }

    @Autowired
    UserService userService;

    @ApiOperation(value = "Allows for the creation of a user Profile")
    @PostMapping("/create")
    public User createUserProfile(@RequestParam(name = "userId") String userId,
                                  @RequestParam(name = "displayName", required = true, defaultValue = "AnonymousDev") String displayName) {

        if (userId.equals("")) throw new BadRequestException("UserId cannot be blank");

        if (userService.checkIfUserIdExits(userId)) {
            throw new UserAlreadyExistsException("Unable to create user with Id { " + userId + " }, the " +
                    "userId already exists");
        }
        return userService.createNewUserAccount(userId, displayName);
    }
}