遇到过这样的问题,使用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"/>