单例bean有多个对象用于序列化@RequestBody

时间:2019-07-16 17:00:10

标签: java spring

我有一个以下的春季托管bean。

@Entity
@Component
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class Offer {
     // fields and getter, setters toString hashtag
}

并跟随端点。

@PostMapping
public ResponseEntity<Offer> publishOffer(@RequestBody Offer offer) throws InterruptedException {
    System.out.println (Thread.currentThread ().getName ());
    System.out.println ("Before thread sleep => " + offer);
    Thread.currentThread ().sleep (90000);
    System.out.println ("After thread sleep => " + offer + "from thread " + Thread.currentThread ().getName ());
    return null;
}

@RequestBody模型,即Offer是Singleton。我有意添加了Sleep(90000)。我在90秒钟内发送了3个垃圾邮件请求。

由于Offer bean是单例bean,因此spring容器中应该只有一个对象。

为什么我得到Offer bean的3个不同的对象?

After thread sleep => Offer{jobTitle='Manager'}from thread http-nio-8080-exec-1
After thread sleep => Offer{jobTitle='Sr. Software 1'}from thread http-nio-8080-exec-2
After thread sleep => Offer{jobTitle='Sr. Software 4'}from thread http-nio-8080-exec-3

1 个答案:

答案 0 :(得分:0)

对象可能由spring根据您的注释进行管理,但是传入的requestBody不是spring托管的bean。看看@john Doe的答案。这很好解释 How to understand Bean in Spring? 为了验证这一点,请尝试从IOC容器中检索bean并进行打印,它应该是单例