JsonSong
JsonSong

Reputation: 348

After add @Transactional on method , this class(service) can't Autowired bean(repository)?

  1. @EnableTransactionManagement is added on MyBootApplication
  2. @Bean public Object testBean(PlatformTransactionManager platformTransactionManager){ System.out.println(">>>>>>>>>>TransactionManager is " + platformTransactionManager.getClass().getName()); return new Object(); } it print TransactionManager is 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) } }

i use spring.boot to build my project.

when i remove the @Transactional,everything is fine .

but when i add it. the repository is null,it can't be inject.

i read that question Using @Transaction annotation with @Autowired - Spring ,and i add spring.aop.proxy-target-class=true in my application.properties ,still same error .

i use kotlin to write it,but i guess this is not the reason.

even so ,i will try with java later .

My mistake , I have some properties in BaseService , should add open on it too .

Upvotes: 3

Views: 1602

Answers (2)

itavq
itavq

Reputation: 135

For others that might miss the resolution in bold at the bottom of the question: All public functions should be set to open (in oppose to the implicit final default in kotlin)

Upvotes: 1

JsonSong
JsonSong

Reputation: 348

I have added 'open' on func yet ,but leave out the propeties .

@PersistenceContext
protected open lateinit var em: EntityManager

@Autowired
protected open lateinit var env: Environment

Upvotes: 1

Related Questions