【问题标题】:After add @Transactional on method , this class(service) can't Autowired bean(repository)?在方法上添加@Transactional后,这个类(服务)不能自动装配bean(存储库)?
【发布时间】:2016-12-04 20:08:57
【问题描述】:
  1. @EnableTransactionManagement 添加到 MyBootApplication
  2. @Bean public Object testBean(PlatformTransactionManager platformTransactionManager){ System.out.println(">>>>>>>>>>TransactionManager is " + platformTransactionManager.getClass().getName()); return new Object(); } 它打印 TransactionManager 是 org.springframework.orm.jpa.JpaTransactionManager
  3. @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) } }

我使用 spring.boot 来构建我的项目。

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

但是当我添加它时。仓库为空,不能注入。

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

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

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

我的错误,我在 BaseService 中有一些属性,也应该添加 open 。

【问题讨论】:

  • 我遇到了同样的问题,因为我使用了 javax.transaction 包中的 @Transactional。更改为 org.springframework.transaction.annotation 后它可以工作。

标签: spring jpa proxy transactional


【解决方案1】:

我已经在 func 上添加了“open”,但忽略了属性。

@PersistenceContext
protected open lateinit var em: EntityManager

@Autowired
protected open lateinit var env: Environment

【讨论】:

    【解决方案2】:

    对于可能错过问题底部粗体解决方案的其他人: 所有公共函数都应设置为打开(与 kotlin 中隐含的最终默认值相反)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      相关资源
      最近更新 更多