2016-09-14 83 views
7

當我把 「@Transactional(readOnly=false)」 標註在我的服務類,我得到以下錯誤事務註釋錯誤

說明:

The bean 'studentService' could not be injected as a ' com.student.service.StudentServiceImpl ' because it is a JDK dynamic proxy that implements: com.student.service.StudentService

示例代碼:

@Service("studentService") 
@Transactional(readOnly=false) 
public class StudentServiceImpl implements StudentService { 

} 

public interface StudentService { 

} 

操作:

考慮將bean注入其中一個接口或強制使用CGLib-通過在@EnableAsync和/或@EnableCaching上設置proxyTargetClass=true來實現基於代理的代理。

進程退出代碼爲1

是什麼原因造成完了?

+0

如果您嘗試使用JRE構建項目,那麼您可以將其更改爲JDK嗎?你的類路徑中是否有所有與方面相關的jar? – Nimesh

+5

這是我的錯誤,我已經自動佈線了Interface的實現類。 – syv

+0

你可以用這個答案回答你自己的問題,因爲我遇到了同樣的事情,這幫助我解決了這個問題。謝謝! – emilyk

回答

6

由於已經在評論中提到過,當您嘗試注入/ autowire實現類而不是接口時會發生錯誤。

The bean 'studentService' could not be injected as a 'com.student.service.StudentServiceImpl' because it is a JDK dynamic proxy that implements: com.student.service.StudentService

上公佈通過這樣的設置,

public class StudentServiceImpl implements StudentService { 
} 

public interface StudentService { 
} 

如果自動裝配界面如下,你不會得到一個錯誤:在春季啓動項目

@Autowired //or @Inject 
StudentService studentService; 
+0

如果界面有很多實現,彈簧如何知道我要注入哪一個? – Nurlan

+1

@Nurlan檢查限定符註釋 –

5

,嘗試加:

spring.aop.proxy-target-class=true 

到你的application.pr operties

OR

@EnableAspectJAutoProxy(proxyTargetClass = true) 

你的春天引導入口點。