Sonarqube在PersistenceContext EntityManager上显示严重漏洞

问题描述

使用Sonarqube扫描Spring Boot项目时,出现以下问题:

@PersistenceContext
private EntityManager em;

问题说:

Spring @ Component,@ Controller,@ Service和@Repository类默认为单例,这意味着在应用程序中仅实例化了该类的一个实例。通常,这样的类可能有一些静态成员,例如logger,但是所有非静态成员都应该由Spring管理。也就是说,它们应具有以下注释之一:@ Resource,@ Inject,@ Autowired或@Value。

在这些类别之一中具有未注入成员可能表示尝试管理状态。由于它们是单例,因此几乎可以保证最终会从User1的会话向User2公开数据。

当单例@ Controller,@ Service或@Repository具有非静态成员且未使用以下任一注释时,此规则会引起问题:

org.springframework.beans.factory.annotation.Autowired

org.springframework.beans.factory.annotation.Value

javax.annotation.Inject

javax.annotation.Resource

那么,这实际上是一个问题吗?还是只是误报?将所有这些查询移至JpaRepository是个好主意吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...