春季-如何检测未正确注入单例Bean中的原型Bean?

时间:2020-06-22 22:35:49

标签: spring singleton prototype

在没有适当注意的情况下将原型bean注入singleton会导致应用程序意外行为。 是否存在任何可以检测/警告此问题的静态代码分析器工具,或者任何人都遇到过类似的问题,并且需要采取哪些措施来防止它发生?

@Service
@Scope(BeanDefinition.PROTOTYPE)
public class PaymentService {

int x;
int y;

pay(a,b) {

}

@Service
public class CustomerService {

@Autowired// This bean is used as a singleton here which will cause erroneous transactions 
private PaymentService paymentService;

public void payCustomer(int a, int b){
paymentService.pay(a, b);
}

0 个答案:

没有答案
相关问题