1. 程式人生 > >解決spring 自動掃描包的時候出現 service無法自動注入的問題

解決spring 自動掃描包的時候出現 service無法自動注入的問題

遇到過這樣的問題,使用spring 將 包自動掃描進來在訪問的時候出現如下報錯:

Error creating bean with name ‘com.xxx.demoController’:
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field:demoService

demoService不能自動注入,後來發現是我在配置定時器的時候,將這個service中的某個方法做了spring的定時任務,相當於顯示的將這個類已經掃描到spring的配置中。這個時候通過自動掃描又將這個類有掃描了一遍,出現了兩個bean ,bean不是唯一的。由於要保留定時任務,所以要將spring中的自動掃描的地方去除關於這個類的掃描,在自動掃描的地方去除衝突的類或者包。

<context:exclude-filter type="regex" expression="com.demo.demoService"/>