潜意识中Spring中Bean是单例的,直到最近被人问起才想到证明下。
具体代码如下:
package com.daojia.score.service.impl;
import com.daojia.score.service.TestService;
import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by zhaoxp on 2017/3/8.
*/
@Service
public class TestServiceImpl implements TestService {
/**
* 定义成private
*/
private AtomicLong atomicLong = new AtomicLong();
public long increment(){
return atomicLong.incrementAndGet();
}
}
调用方代码如下:
package com.daojia.score.controller;
import com.daojia.score.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.Res