本来是一个特别小的错误,因为实体类没有加注解@Component,导致在controller层找不到该实体类也就无法注入。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '*****Controller': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cz.czPay.system.entity.*****] found for dependency [com.cz.czPay.system.entity.BalanceDetail]: expected at least 1 bean which qualifies as autowire candidate for this dependency

该错误就是无法创建bean在*****Controller中,依赖注入失败。找不到bean找不到类型为*****来进行依赖注入

 

 

还有其他原因导致无法注入问题比如配置application.xml这样就不需要加@Component注解都可以实现注入。

但是这样会导致一个问题,在spring中默认是单例模式,所以实体类如果通过依赖注入的话,多次调用实体类一定会造成该实体类属性的信息混乱。

单例:Spring中因为有依赖注入所以对于service可以通过注入只创建一个该对象。但是如果实体类单例的话,可想而知很多个调用都调用者一个实体类,它内部值会被多个controller更改一定会信息混乱所以不建议spring实行component注解依赖注入使用。

多例:可以通过多次new创建多个对象,对于实体类来说,有用到需要的地方再创建该实体类,保证了信息安全性。