变量被覆盖

时间:2017-06-24 10:33:28

标签: java spring-boot dependency-injection lombok

我使用Spring Boot和Lombok编写了一些代码。我有两个对象,Zend\ServiceManager#getServiceLocator()user都基于loggedInUser。我为每个对象User.class字段分配了不同的值。我得到了以下代码。 userEmail是端点请求中的内容,而user是执行请求的用户。

这是loggedInUser

User.class

这是我正在使用@Component @Data public class User { private String userEmail; }

的课程
User.class

return(new ResponseEntity(users,headers,HttpStatus.OK));     }

它给了我以下输出。

private User user;

private User loggedInUser;

private SearchParameters searchParameters;

public UserController(
        User user,
        User loggedInUser,
        SearchParameters searchParameters, {

    this.user = user;
    this.loggedInUser = loggedInUser;
    this.searchParameters = searchParameters;
}

@GetMapping(value = "${apiVersion.v_1}" + "/users")
public ResponseEntity getUsers(
        @RequestParam("userEmail") String userEmail,
        @RequestParam("direction") String direction,
        @RequestParam("limit") String limit) {

    Jws<Claims> jwsClaims = (Jws<Claims>) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    loggedInUser.setUserEmail(jwsClaims.getBody().getSubject()); //From a JWT the logged in user's e-mail is picked up. In this case joe@example.com
    log.info("loggedInUser.getUserEmail #1: " + loggedInUser.getUserEmail());

    user.setUserEmail(userEmail); // From the request parameters the email is that will be used for the search is picked up. In this case smith@example.com
    searchParameters.setDirection(direction); //Can be "u" or "d" that corresponds to "SELECT * FROM user_tbl WHERE < user.getEmail()" or "SELECT * FROM user_tbl WHERE > user.getEmail()" in the SQL that will run in another class 
    searchParameters.setLimit(limit); //An integer that will limit the number of returned results from the user_tbl
    log.info("loggedInUser.getUserEmail #2: " + loggedInUser.getUserEmail());

    //The search is done in another class that is taking user.getEmail() as an argument and returns a List of users

我原以为loggedInUser.getUserEmail #1: joe@example.com loggedInUser.getUserEmail #2: smith@example.com user是两个独立的对象,因为它们有不同的名称,并且依赖注入每次实例化一个新对象,但似乎loggedInUser是覆盖user。我在这里缺少什么?

0 个答案:

没有答案