在方法上添加@Transactional后,这个类(服务)不能自动连接bean(存储库)?

时间:2016-07-30 03:42:50

标签: spring jpa proxy transactional

  1. 在MyBootApplication
  2. 上添加了@EnableTransactionManagement
  3. @Bean public Object testBean(PlatformTransactionManager platformTransactionManager){ System.out.println(">>>>>>>>>>TransactionManager is " + platformTransactionManager.getClass().getName()); return new Object(); } 它打印TransactionManager是org.springframework.orm.jpa.JpaTransactionManager
  4. @Service open class UserService : BaseService() { @Autowired lateinit var repository: UserRepository @Transactional fun updateValid(id: Long, valid: Boolean) { ErrorConstant.ParamErrorCode.IdIsNull.caseThrow { id == 0.toLong() } repository.updateValid(id,valid) } }
  5. 我使用spring.boot来构建我的项目。

    当我删除@Transactional时,一切都很好。

    但是当我添加它。存储库为null,无法注入。

    我读了那个问题Using @Transaction annotation with @Autowired - Spring,我在我的application.properties中添加了spring.aop.proxy-target-class=true,仍然是同样的错误。

    我用kotlin写它,但我猜这不是原因。

    即便如此,我稍后会尝试使用java。

    我的错误,我在BaseService中有一些属性,也应该在它上面打开。

2 个答案:

答案 0 :(得分:1)

我已经在func上添加了'open',但是遗漏了这些内容。

@PersistenceContext
protected open lateinit var em: EntityManager

@Autowired
protected open lateinit var env: Environment

答案 1 :(得分:0)

对于其他可能会错过问题底部以粗体显示的分辨率的用户: 所有公共功能都应设置为打开(与kotlin中的隐式最终默认设置相反)